Skip to content

Commit da1bd03

Browse files
authored
chore: annotate sync tests with types (microsoft#882)
1 parent 79e77e9 commit da1bd03

38 files changed

+608
-372
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ repos:
1717
rev: v0.910
1818
hooks:
1919
- id: mypy
20+
additional_dependencies: [types-pyOpenSSL==20.0.6]
2021
- repo: https://gitlab.com/pycqa/flake8
2122
rev: 3.9.2
2223
hooks:

local-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ service_identity==21.1.0
2121
setuptools==57.4.0
2222
twine==3.4.2
2323
twisted==21.7.0
24+
types-pyOpenSSL==20.0.6
2425
wheel==0.37.0

playwright/_impl/_locator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async def evaluate(
141141
timeout,
142142
)
143143

144-
async def evaluate_all(self, expression: str, arg: Serializable = None) -> None:
144+
async def evaluate_all(self, expression: str, arg: Serializable = None) -> Any:
145145
params = locals_to_params(locals())
146146
return await self._frame.eval_on_selector_all(self._selector, **params)
147147

playwright/async_api/_generated.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11338,7 +11338,7 @@ async def evaluate(
1133811338
)
1133911339
)
1134011340

11341-
async def evaluate_all(self, expression: str, arg: typing.Any = None) -> NoneType:
11341+
async def evaluate_all(self, expression: str, arg: typing.Any = None) -> typing.Any:
1134211342
"""Locator.evaluate_all
1134311343
1134411344
The method finds all elements matching the specified locator and passes an array of matched elements as a first argument
@@ -11361,6 +11361,10 @@ async def evaluate_all(self, expression: str, arg: typing.Any = None) -> NoneTyp
1136111361
as a function. Otherwise, evaluated as an expression.
1136211362
arg : Union[Any, NoneType]
1136311363
Optional argument to pass to `expression`.
11364+
11365+
Returns
11366+
-------
11367+
Any
1136411368
"""
1136511369

1136611370
return mapping.from_maybe_impl(

playwright/sync_api/_generated.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11268,7 +11268,7 @@ def evaluate(
1126811268
)
1126911269
)
1127011270

11271-
def evaluate_all(self, expression: str, arg: typing.Any = None) -> NoneType:
11271+
def evaluate_all(self, expression: str, arg: typing.Any = None) -> typing.Any:
1127211272
"""Locator.evaluate_all
1127311273
1127411274
The method finds all elements matching the specified locator and passes an array of matched elements as a first argument
@@ -11291,6 +11291,10 @@ def evaluate_all(self, expression: str, arg: typing.Any = None) -> NoneType:
1129111291
as a function. Otherwise, evaluated as an expression.
1129211292
arg : Union[Any, NoneType]
1129311293
Optional argument to pass to `expression`.
11294+
11295+
Returns
11296+
-------
11297+
Any
1129411298
"""
1129511299

1129611300
return mapping.from_maybe_impl(

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ warn_redundant_casts = True
1414
warn_unused_configs = True
1515
check_untyped_defs = True
1616
disallow_untyped_defs = True
17-
[mypy-tests.*]
17+
[mypy-tests/async.*]
1818
ignore_errors = True
1919
[flake8]
2020
ignore =

tests/async/test_element_handle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ async def test_bounding_box_with_SVG_nodes(page, server):
7070
assert pw_bounding_box == web_bounding_box
7171

7272

73+
@pytest.mark.skip_browser("firefox")
7374
async def test_bounding_box_with_page_scale(browser, server):
7475
context = await browser.new_context(
75-
viewport={"width": 400, "height": 400, "is_mobile": True}
76+
viewport={"width": 400, "height": 400}, is_mobile=True
7677
)
7778
page = await context.new_page()
7879
await page.goto(server.PREFIX + "/input/button.html")

tests/common/test_threads.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
# limitations under the License.
1414

1515
import threading
16+
from typing import Dict
1617

1718
from playwright.sync_api import sync_playwright
1819

1920

20-
def test_running_in_thread(browser_name, launch_arguments):
21+
def test_running_in_thread(browser_name: str, launch_arguments: Dict) -> None:
2122
result = []
2223

2324
class TestThread(threading.Thread):
24-
def run(self):
25+
def run(self) -> None:
2526
with sync_playwright() as playwright:
2627
browser = getattr(playwright, browser_name).launch(**launch_arguments)
2728
# This should not throw ^^.

tests/conftest.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import subprocess
2020
import sys
2121
from pathlib import Path
22-
from typing import Dict, List
22+
from typing import Any, AsyncGenerator, Callable, Dict, Generator, List
2323

2424
import pytest
2525
from PIL import Image
@@ -29,106 +29,108 @@
2929
import playwright
3030
from playwright._impl._path_utils import get_file_dirname
3131

32-
from .server import test_server
32+
from .server import Server, WebSocketServerServer, test_server
3333

3434
_dirname = get_file_dirname()
3535

3636

37-
def pytest_generate_tests(metafunc):
37+
def pytest_generate_tests(metafunc: Any) -> None:
3838
if "browser_name" in metafunc.fixturenames:
3939
browsers = metafunc.config.option.browser or ["chromium", "firefox", "webkit"]
4040
metafunc.parametrize("browser_name", browsers, scope="session")
4141

4242

4343
@pytest.fixture(scope="session")
44-
def event_loop():
44+
def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
4545
loop = asyncio.get_event_loop()
4646
yield loop
4747
loop.close()
4848

4949

5050
@pytest.fixture(scope="session")
51-
def assetdir():
51+
def assetdir() -> Path:
5252
return _dirname / "assets"
5353

5454

5555
@pytest.fixture(scope="session")
56-
def launch_arguments(pytestconfig):
56+
def launch_arguments(pytestconfig: Any) -> Dict:
5757
return {
5858
"headless": not pytestconfig.getoption("--headful"),
5959
"channel": pytestconfig.getoption("--browser-channel"),
6060
}
6161

6262

6363
@pytest.fixture
64-
def server():
64+
def server() -> Generator[Server, None, None]:
6565
yield test_server.server
6666

6767

6868
@pytest.fixture
69-
def https_server():
69+
def https_server() -> Generator[Server, None, None]:
7070
yield test_server.https_server
7171

7272

7373
@pytest.fixture
74-
def ws_server():
74+
def ws_server() -> Generator[WebSocketServerServer, None, None]:
7575
yield test_server.ws_server
7676

7777

7878
@pytest.fixture(autouse=True, scope="session")
79-
async def start_server():
79+
async def start_server() -> AsyncGenerator[None, None]:
8080
test_server.start()
8181
yield
8282
test_server.stop()
8383

8484

8585
@pytest.fixture(autouse=True)
86-
def after_each_hook():
86+
def after_each_hook() -> Generator[None, None, None]:
8787
yield
8888
test_server.reset()
8989

9090

9191
@pytest.fixture(scope="session")
92-
def browser_name(pytestconfig):
92+
def browser_name(pytestconfig: Any) -> None:
9393
return pytestconfig.getoption("browser")
9494

9595

9696
@pytest.fixture(scope="session")
97-
def browser_channel(pytestconfig):
97+
def browser_channel(pytestconfig: Any) -> None:
9898
return pytestconfig.getoption("--browser-channel")
9999

100100

101101
@pytest.fixture(scope="session")
102-
def is_webkit(browser_name):
102+
def is_webkit(browser_name: str) -> bool:
103103
return browser_name == "webkit"
104104

105105

106106
@pytest.fixture(scope="session")
107-
def is_firefox(browser_name):
107+
def is_firefox(browser_name: str) -> bool:
108108
return browser_name == "firefox"
109109

110110

111111
@pytest.fixture(scope="session")
112-
def is_chromium(browser_name):
112+
def is_chromium(browser_name: str) -> bool:
113113
return browser_name == "chromium"
114114

115115

116116
@pytest.fixture(scope="session")
117-
def is_win():
117+
def is_win() -> bool:
118118
return sys.platform == "win32"
119119

120120

121121
@pytest.fixture(scope="session")
122-
def is_linux():
122+
def is_linux() -> bool:
123123
return sys.platform == "linux"
124124

125125

126126
@pytest.fixture(scope="session")
127-
def is_mac():
127+
def is_mac() -> bool:
128128
return sys.platform == "darwin"
129129

130130

131-
def _get_skiplist(request, values, value_name):
131+
def _get_skiplist(
132+
request: pytest.FixtureRequest, values: List[str], value_name: str
133+
) -> List[str]:
132134
skipped_values = []
133135
# Allowlist
134136
only_marker = request.node.get_closest_marker(f"only_{value_name}")
@@ -145,7 +147,7 @@ def _get_skiplist(request, values, value_name):
145147

146148

147149
@pytest.fixture(autouse=True)
148-
def skip_by_browser(request, browser_name):
150+
def skip_by_browser(request: pytest.FixtureRequest, browser_name: str) -> None:
149151
skip_browsers_names = _get_skiplist(
150152
request, ["chromium", "firefox", "webkit"], "browser"
151153
)
@@ -155,7 +157,7 @@ def skip_by_browser(request, browser_name):
155157

156158

157159
@pytest.fixture(autouse=True)
158-
def skip_by_platform(request):
160+
def skip_by_platform(request: pytest.FixtureRequest) -> None:
159161
skip_platform_names = _get_skiplist(
160162
request, ["win32", "linux", "darwin"], "platform"
161163
)
@@ -164,7 +166,7 @@ def skip_by_platform(request):
164166
pytest.skip(f"skipped on this platform: {sys.platform}")
165167

166168

167-
def pytest_addoption(parser):
169+
def pytest_addoption(parser: Any) -> None:
168170
group = parser.getgroup("playwright", "Playwright")
169171
group.addoption(
170172
"--browser",
@@ -187,8 +189,8 @@ def pytest_addoption(parser):
187189

188190

189191
@pytest.fixture(scope="session")
190-
def assert_to_be_golden(browser_name: str):
191-
def compare(received_raw: bytes, golden_name: str):
192+
def assert_to_be_golden(browser_name: str) -> Callable[[bytes, str], None]:
193+
def compare(received_raw: bytes, golden_name: str) -> None:
192194
golden_file = (_dirname / f"golden-{browser_name}" / golden_name).read_bytes()
193195
received_image = Image.open(io.BytesIO(received_raw))
194196
golden_image = Image.open(io.BytesIO(golden_file))
@@ -235,7 +237,7 @@ def __init__(
235237
self.ws_endpoint = self.process.stdout.readline().decode().strip()
236238
self.process.stdout.close()
237239

238-
def kill(self):
240+
def kill(self) -> None:
239241
# Send the signal to all the process groups
240242
if self.process.poll() is not None:
241243
return
@@ -244,10 +246,12 @@ def kill(self):
244246

245247

246248
@pytest.fixture
247-
def launch_server(browser_name: str, launch_arguments: Dict, tmp_path: Path):
249+
def launch_server(
250+
browser_name: str, launch_arguments: Dict, tmp_path: Path
251+
) -> Generator[Callable[..., RemoteServer], None, None]:
248252
remotes: List[RemoteServer] = []
249253

250-
def _launch_server(**kwargs: Dict):
254+
def _launch_server(**kwargs: Dict[str, Any]) -> RemoteServer:
251255
remote = RemoteServer(
252256
browser_name,
253257
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy