Skip to content

Commit 1bf55d7

Browse files
authored
chore: fix tests/async pyright linter issues (microsoft#2175)
1 parent 85df32c commit 1bf55d7

File tree

80 files changed

+3234
-1941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+3234
-1941
lines changed

tests/async/conftest.py

+35-14
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,60 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
15+
import asyncio
16+
from typing import Any, AsyncGenerator, Awaitable, Callable, Dict, Generator, List
1617

17-
from playwright.async_api import async_playwright
18+
import pytest
1819

20+
from playwright.async_api import (
21+
Browser,
22+
BrowserContext,
23+
BrowserType,
24+
Page,
25+
Playwright,
26+
Selectors,
27+
async_playwright,
28+
)
29+
30+
from .utils import Utils
1931
from .utils import utils as utils_object
2032

2133

2234
@pytest.fixture
23-
def utils():
35+
def utils() -> Generator[Utils, None, None]:
2436
yield utils_object
2537

2638

2739
# Will mark all the tests as async
28-
def pytest_collection_modifyitems(items):
40+
def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
2941
for item in items:
3042
item.add_marker(pytest.mark.asyncio)
3143

3244

3345
@pytest.fixture(scope="session")
34-
async def playwright():
46+
async def playwright() -> AsyncGenerator[Playwright, None]:
3547
async with async_playwright() as playwright_object:
3648
yield playwright_object
3749

3850

3951
@pytest.fixture(scope="session")
40-
def browser_type(playwright, browser_name: str):
52+
def browser_type(playwright: Playwright, browser_name: str) -> BrowserType:
4153
if browser_name == "chromium":
4254
return playwright.chromium
4355
if browser_name == "firefox":
4456
return playwright.firefox
4557
if browser_name == "webkit":
4658
return playwright.webkit
59+
raise Exception(f"Invalid browser_name: {browser_name}")
4760

4861

4962
@pytest.fixture(scope="session")
50-
async def browser_factory(launch_arguments, browser_type):
63+
async def browser_factory(
64+
launch_arguments: Dict, browser_type: BrowserType
65+
) -> AsyncGenerator[Callable[..., Awaitable[Browser]], None]:
5166
browsers = []
5267

53-
async def launch(**kwargs):
68+
async def launch(**kwargs: Any) -> Browser:
5469
browser = await browser_type.launch(**launch_arguments, **kwargs)
5570
browsers.append(browser)
5671
return browser
@@ -61,17 +76,21 @@ async def launch(**kwargs):
6176

6277

6378
@pytest.fixture(scope="session")
64-
async def browser(browser_factory):
79+
async def browser(
80+
browser_factory: "Callable[..., asyncio.Future[Browser]]",
81+
) -> AsyncGenerator[Browser, None]:
6582
browser = await browser_factory()
6683
yield browser
6784
await browser.close()
6885

6986

7087
@pytest.fixture
71-
async def context_factory(browser):
88+
async def context_factory(
89+
browser: Browser,
90+
) -> AsyncGenerator["Callable[..., Awaitable[BrowserContext]]", None]:
7291
contexts = []
7392

74-
async def launch(**kwargs):
93+
async def launch(**kwargs: Any) -> BrowserContext:
7594
context = await browser.new_context(**kwargs)
7695
contexts.append(context)
7796
return context
@@ -82,19 +101,21 @@ async def launch(**kwargs):
82101

83102

84103
@pytest.fixture
85-
async def context(context_factory):
104+
async def context(
105+
context_factory: "Callable[..., asyncio.Future[BrowserContext]]",
106+
) -> AsyncGenerator[BrowserContext, None]:
86107
context = await context_factory()
87108
yield context
88109
await context.close()
89110

90111

91112
@pytest.fixture
92-
async def page(context):
113+
async def page(context: BrowserContext) -> AsyncGenerator[Page, None]:
93114
page = await context.new_page()
94115
yield page
95116
await page.close()
96117

97118

98119
@pytest.fixture(scope="session")
99-
def selectors(playwright):
120+
def selectors(playwright: Playwright) -> Selectors:
100121
return playwright.selectors

tests/async/test_accessibility.py

+46-29
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
import pytest
1919

20+
from playwright.async_api import Page
2021

21-
async def test_accessibility_should_work(page, is_firefox, is_chromium):
22+
23+
async def test_accessibility_should_work(
24+
page: Page, is_firefox: bool, is_chromium: bool
25+
) -> None:
2226
await page.set_content(
2327
"""<head>
2428
<title>Accessibility Test</title>
@@ -109,54 +113,62 @@ async def test_accessibility_should_work(page, is_firefox, is_chromium):
109113
assert await page.accessibility.snapshot() == golden
110114

111115

112-
async def test_accessibility_should_work_with_regular_text(page, is_firefox):
116+
async def test_accessibility_should_work_with_regular_text(
117+
page: Page, is_firefox: bool
118+
) -> None:
113119
await page.set_content("<div>Hello World</div>")
114120
snapshot = await page.accessibility.snapshot()
121+
assert snapshot
115122
assert snapshot["children"][0] == {
116123
"role": "text leaf" if is_firefox else "text",
117124
"name": "Hello World",
118125
}
119126

120127

121-
async def test_accessibility_roledescription(page):
128+
async def test_accessibility_roledescription(page: Page) -> None:
122129
await page.set_content('<p tabIndex=-1 aria-roledescription="foo">Hi</p>')
123130
snapshot = await page.accessibility.snapshot()
131+
assert snapshot
124132
assert snapshot["children"][0]["roledescription"] == "foo"
125133

126134

127-
async def test_accessibility_orientation(page):
135+
async def test_accessibility_orientation(page: Page) -> None:
128136
await page.set_content(
129137
'<a href="" role="slider" aria-orientation="vertical">11</a>'
130138
)
131139
snapshot = await page.accessibility.snapshot()
140+
assert snapshot
132141
assert snapshot["children"][0]["orientation"] == "vertical"
133142

134143

135-
async def test_accessibility_autocomplete(page):
144+
async def test_accessibility_autocomplete(page: Page) -> None:
136145
await page.set_content('<div role="textbox" aria-autocomplete="list">hi</div>')
137146
snapshot = await page.accessibility.snapshot()
147+
assert snapshot
138148
assert snapshot["children"][0]["autocomplete"] == "list"
139149

140150

141-
async def test_accessibility_multiselectable(page):
151+
async def test_accessibility_multiselectable(page: Page) -> None:
142152
await page.set_content(
143153
'<div role="grid" tabIndex=-1 aria-multiselectable=true>hey</div>'
144154
)
145155
snapshot = await page.accessibility.snapshot()
156+
assert snapshot
146157
assert snapshot["children"][0]["multiselectable"]
147158

148159

149-
async def test_accessibility_keyshortcuts(page):
160+
async def test_accessibility_keyshortcuts(page: Page) -> None:
150161
await page.set_content(
151162
'<div role="grid" tabIndex=-1 aria-keyshortcuts="foo">hey</div>'
152163
)
153164
snapshot = await page.accessibility.snapshot()
165+
assert snapshot
154166
assert snapshot["children"][0]["keyshortcuts"] == "foo"
155167

156168

157169
async def test_accessibility_filtering_children_of_leaf_nodes_should_not_report_text_nodes_inside_controls(
158-
page, is_firefox
159-
):
170+
page: Page, is_firefox: bool
171+
) -> None:
160172
await page.set_content(
161173
"""
162174
<div role="tablist">
@@ -179,13 +191,14 @@ async def test_accessibility_filtering_children_of_leaf_nodes_should_not_report_
179191
# WebKit rich text accessibility is iffy
180192
@pytest.mark.only_browser("chromium")
181193
async def test_accessibility_plain_text_field_with_role_should_not_have_children(
182-
page, browser_channel
183-
):
194+
page: Page,
195+
) -> None:
184196
await page.set_content(
185197
"""
186198
<div contenteditable="plaintext-only" role='textbox'>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
187199
)
188200
snapshot = await page.accessibility.snapshot()
201+
assert snapshot
189202
assert snapshot["children"][0] == {
190203
"multiline": True,
191204
"name": "",
@@ -196,13 +209,14 @@ async def test_accessibility_plain_text_field_with_role_should_not_have_children
196209

197210
@pytest.mark.only_browser("chromium")
198211
async def test_accessibility_plain_text_field_without_role_should_not_have_content(
199-
page, browser_channel
200-
):
212+
page: Page,
213+
) -> None:
201214
await page.set_content(
202215
"""
203216
<div contenteditable="plaintext-only">Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
204217
)
205218
snapshot = await page.accessibility.snapshot()
219+
assert snapshot
206220
assert snapshot["children"][0] == {
207221
"name": "",
208222
"role": "generic",
@@ -212,13 +226,14 @@ async def test_accessibility_plain_text_field_without_role_should_not_have_conte
212226

213227
@pytest.mark.only_browser("chromium")
214228
async def test_accessibility_plain_text_field_with_tabindex_and_without_role_should_not_have_content(
215-
page, browser_channel
216-
):
229+
page: Page,
230+
) -> None:
217231
await page.set_content(
218232
"""
219233
<div contenteditable="plaintext-only" tabIndex=0>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
220234
)
221235
snapshot = await page.accessibility.snapshot()
236+
assert snapshot
222237
assert snapshot["children"][0] == {
223238
"name": "",
224239
"role": "generic",
@@ -227,8 +242,8 @@ async def test_accessibility_plain_text_field_with_tabindex_and_without_role_sho
227242

228243

229244
async def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_label_should_not_have_children(
230-
page, is_chromium, is_firefox
231-
):
245+
page: Page, is_chromium: bool, is_firefox: bool
246+
) -> None:
232247
await page.set_content(
233248
"""
234249
<div role="textbox" tabIndex=0 aria-checked="true" aria-label="my favorite textbox">
@@ -255,12 +270,13 @@ async def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_lab
255270
"value": "this is the inner content ",
256271
}
257272
snapshot = await page.accessibility.snapshot()
273+
assert snapshot
258274
assert snapshot["children"][0] == golden
259275

260276

261277
async def test_accessibility_checkbox_with_and_tabIndex_and_label_should_not_have_children(
262-
page,
263-
):
278+
page: Page,
279+
) -> None:
264280
await page.set_content(
265281
"""
266282
<div role="checkbox" tabIndex=0 aria-checked="true" aria-label="my favorite checkbox">
@@ -270,12 +286,13 @@ async def test_accessibility_checkbox_with_and_tabIndex_and_label_should_not_hav
270286
)
271287
golden = {"role": "checkbox", "name": "my favorite checkbox", "checked": True}
272288
snapshot = await page.accessibility.snapshot()
289+
assert snapshot
273290
assert snapshot["children"][0] == golden
274291

275292

276293
async def test_accessibility_checkbox_without_label_should_not_have_children(
277-
page, is_firefox
278-
):
294+
page: Page, is_firefox: bool
295+
) -> None:
279296
await page.set_content(
280297
"""
281298
<div role="checkbox" aria-checked="true">
@@ -289,10 +306,11 @@ async def test_accessibility_checkbox_without_label_should_not_have_children(
289306
"checked": True,
290307
}
291308
snapshot = await page.accessibility.snapshot()
309+
assert snapshot
292310
assert snapshot["children"][0] == golden
293311

294312

295-
async def test_accessibility_should_work_a_button(page):
313+
async def test_accessibility_should_work_a_button(page: Page) -> None:
296314
await page.set_content("<button>My Button</button>")
297315

298316
button = await page.query_selector("button")
@@ -302,7 +320,7 @@ async def test_accessibility_should_work_a_button(page):
302320
}
303321

304322

305-
async def test_accessibility_should_work_an_input(page):
323+
async def test_accessibility_should_work_an_input(page: Page) -> None:
306324
await page.set_content('<input title="My Input" value="My Value">')
307325

308326
input = await page.query_selector("input")
@@ -313,9 +331,7 @@ async def test_accessibility_should_work_an_input(page):
313331
}
314332

315333

316-
async def test_accessibility_should_work_on_a_menu(
317-
page, is_webkit, is_chromium, browser_channel
318-
):
334+
async def test_accessibility_should_work_on_a_menu(page: Page) -> None:
319335
await page.set_content(
320336
"""
321337
<div role="menu" title="My Menu">
@@ -345,15 +361,15 @@ async def test_accessibility_should_work_on_a_menu(
345361

346362

347363
async def test_accessibility_should_return_null_when_the_element_is_no_longer_in_DOM(
348-
page,
349-
):
364+
page: Page,
365+
) -> None:
350366
await page.set_content("<button>My Button</button>")
351367
button = await page.query_selector("button")
352368
await page.eval_on_selector("button", "button => button.remove()")
353369
assert await page.accessibility.snapshot(root=button) is None
354370

355371

356-
async def test_accessibility_should_show_uninteresting_nodes(page):
372+
async def test_accessibility_should_show_uninteresting_nodes(page: Page) -> None:
357373
await page.set_content(
358374
"""
359375
<div id="root" role="textbox">
@@ -369,6 +385,7 @@ async def test_accessibility_should_show_uninteresting_nodes(page):
369385

370386
root = await page.query_selector("#root")
371387
snapshot = await page.accessibility.snapshot(root=root, interesting_only=False)
388+
assert snapshot
372389
assert snapshot["role"] == "textbox"
373390
assert "hello" in snapshot["value"]
374391
assert "world" in snapshot["value"]

0 commit comments

Comments
 (0)