Skip to content

Commit fa71145

Browse files
authored
chore: use Sequence for input List like types (#2178)
1 parent 1bf55d7 commit fa71145

22 files changed

+481
-407
lines changed

playwright/_impl/_api_structures.py

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

1515
import sys
16-
from typing import Any, Dict, List, Optional, Union
16+
from typing import Any, Dict, List, Optional, Sequence, Union
1717

1818
if sys.version_info >= (3, 8): # pragma: no cover
1919
from typing import Literal, TypedDict
@@ -185,7 +185,7 @@ class ExpectedTextValue(TypedDict, total=False):
185185

186186
class FrameExpectOptions(TypedDict, total=False):
187187
expressionArg: Any
188-
expectedText: Optional[List[ExpectedTextValue]]
188+
expectedText: Optional[Sequence[ExpectedTextValue]]
189189
expectedNumber: Optional[float]
190190
expectedValue: Optional[Any]
191191
useInnerText: Optional[bool]

playwright/_impl/_assertions.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, List, Optional, Pattern, Union
15+
import collections.abc
16+
from typing import Any, List, Optional, Pattern, Sequence, Union
1617
from urllib.parse import urljoin
1718

1819
from playwright._impl._api_structures import ExpectedTextValue, FrameExpectOptions
@@ -149,9 +150,9 @@ def _not(self) -> "LocatorAssertions":
149150
async def to_contain_text(
150151
self,
151152
expected: Union[
152-
List[str],
153-
List[Pattern[str]],
154-
List[Union[Pattern[str], str]],
153+
Sequence[str],
154+
Sequence[Pattern[str]],
155+
Sequence[Union[Pattern[str], str]],
155156
Pattern[str],
156157
str,
157158
],
@@ -160,7 +161,9 @@ async def to_contain_text(
160161
ignore_case: bool = None,
161162
) -> None:
162163
__tracebackhide__ = True
163-
if isinstance(expected, list):
164+
if isinstance(expected, collections.abc.Sequence) and not isinstance(
165+
expected, str
166+
):
164167
expected_text = to_expected_text_values(
165168
expected,
166169
match_substring=True,
@@ -198,9 +201,9 @@ async def to_contain_text(
198201
async def not_to_contain_text(
199202
self,
200203
expected: Union[
201-
List[str],
202-
List[Pattern[str]],
203-
List[Union[Pattern[str], str]],
204+
Sequence[str],
205+
Sequence[Pattern[str]],
206+
Sequence[Union[Pattern[str], str]],
204207
Pattern[str],
205208
str,
206209
],
@@ -244,16 +247,18 @@ async def not_to_have_attribute(
244247
async def to_have_class(
245248
self,
246249
expected: Union[
247-
List[str],
248-
List[Pattern[str]],
249-
List[Union[Pattern[str], str]],
250+
Sequence[str],
251+
Sequence[Pattern[str]],
252+
Sequence[Union[Pattern[str], str]],
250253
Pattern[str],
251254
str,
252255
],
253256
timeout: float = None,
254257
) -> None:
255258
__tracebackhide__ = True
256-
if isinstance(expected, list):
259+
if isinstance(expected, collections.abc.Sequence) and not isinstance(
260+
expected, str
261+
):
257262
expected_text = to_expected_text_values(expected)
258263
await self._expect_impl(
259264
"to.have.class.array",
@@ -273,9 +278,9 @@ async def to_have_class(
273278
async def not_to_have_class(
274279
self,
275280
expected: Union[
276-
List[str],
277-
List[Pattern[str]],
278-
List[Union[Pattern[str], str]],
281+
Sequence[str],
282+
Sequence[Pattern[str]],
283+
Sequence[Union[Pattern[str], str]],
279284
Pattern[str],
280285
str,
281286
],
@@ -402,7 +407,9 @@ async def not_to_have_value(
402407

403408
async def to_have_values(
404409
self,
405-
values: Union[List[str], List[Pattern[str]], List[Union[Pattern[str], str]]],
410+
values: Union[
411+
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
412+
],
406413
timeout: float = None,
407414
) -> None:
408415
__tracebackhide__ = True
@@ -416,7 +423,9 @@ async def to_have_values(
416423

417424
async def not_to_have_values(
418425
self,
419-
values: Union[List[str], List[Pattern[str]], List[Union[Pattern[str], str]]],
426+
values: Union[
427+
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
428+
],
420429
timeout: float = None,
421430
) -> None:
422431
__tracebackhide__ = True
@@ -425,9 +434,9 @@ async def not_to_have_values(
425434
async def to_have_text(
426435
self,
427436
expected: Union[
428-
List[str],
429-
List[Pattern[str]],
430-
List[Union[Pattern[str], str]],
437+
Sequence[str],
438+
Sequence[Pattern[str]],
439+
Sequence[Union[Pattern[str], str]],
431440
Pattern[str],
432441
str,
433442
],
@@ -436,7 +445,9 @@ async def to_have_text(
436445
ignore_case: bool = None,
437446
) -> None:
438447
__tracebackhide__ = True
439-
if isinstance(expected, list):
448+
if isinstance(expected, collections.abc.Sequence) and not isinstance(
449+
expected, str
450+
):
440451
expected_text = to_expected_text_values(
441452
expected,
442453
normalize_white_space=True,
@@ -470,9 +481,9 @@ async def to_have_text(
470481
async def not_to_have_text(
471482
self,
472483
expected: Union[
473-
List[str],
474-
List[Pattern[str]],
475-
List[Union[Pattern[str], str]],
484+
Sequence[str],
485+
Sequence[Pattern[str]],
486+
Sequence[Union[Pattern[str], str]],
476487
Pattern[str],
477488
str,
478489
],
@@ -758,11 +769,13 @@ def expected_regex(
758769

759770

760771
def to_expected_text_values(
761-
items: Union[List[Pattern[str]], List[str], List[Union[str, Pattern[str]]]],
772+
items: Union[
773+
Sequence[Pattern[str]], Sequence[str], Sequence[Union[str, Pattern[str]]]
774+
],
762775
match_substring: bool = False,
763776
normalize_white_space: bool = False,
764777
ignore_case: Optional[bool] = None,
765-
) -> List[ExpectedTextValue]:
778+
) -> Sequence[ExpectedTextValue]:
766779
out: List[ExpectedTextValue] = []
767780
assert isinstance(items, list)
768781
for item in items:

playwright/_impl/_browser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import json
1616
from pathlib import Path
1717
from types import SimpleNamespace
18-
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast
18+
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Sequence, Union, cast
1919

2020
from playwright._impl._api_structures import (
2121
Geolocation,
@@ -96,7 +96,7 @@ async def new_context(
9696
locale: str = None,
9797
timezoneId: str = None,
9898
geolocation: Geolocation = None,
99-
permissions: List[str] = None,
99+
permissions: Sequence[str] = None,
100100
extraHTTPHeaders: Dict[str, str] = None,
101101
offline: bool = None,
102102
httpCredentials: HttpCredentials = None,
@@ -141,7 +141,7 @@ async def new_page(
141141
locale: str = None,
142142
timezoneId: str = None,
143143
geolocation: Geolocation = None,
144-
permissions: List[str] = None,
144+
permissions: Sequence[str] = None,
145145
extraHTTPHeaders: Dict[str, str] = None,
146146
offline: bool = None,
147147
httpCredentials: HttpCredentials = None,
@@ -200,7 +200,7 @@ async def start_tracing(
200200
page: Page = None,
201201
path: Union[str, Path] = None,
202202
screenshots: bool = None,
203-
categories: List[str] = None,
203+
categories: Sequence[str] = None,
204204
) -> None:
205205
params = locals_to_params(locals())
206206
if page:

playwright/_impl/_browser_context.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
List,
2626
Optional,
2727
Pattern,
28+
Sequence,
2829
Set,
2930
Union,
3031
cast,
@@ -284,21 +285,21 @@ async def new_page(self) -> Page:
284285
raise Error("Please use browser.new_context()")
285286
return from_channel(await self._channel.send("newPage"))
286287

287-
async def cookies(self, urls: Union[str, List[str]] = None) -> List[Cookie]:
288+
async def cookies(self, urls: Union[str, Sequence[str]] = None) -> List[Cookie]:
288289
if urls is None:
289290
urls = []
290-
if not isinstance(urls, list):
291+
if isinstance(urls, str):
291292
urls = [urls]
292293
return await self._channel.send("cookies", dict(urls=urls))
293294

294-
async def add_cookies(self, cookies: List[SetCookieParam]) -> None:
295+
async def add_cookies(self, cookies: Sequence[SetCookieParam]) -> None:
295296
await self._channel.send("addCookies", dict(cookies=cookies))
296297

297298
async def clear_cookies(self) -> None:
298299
await self._channel.send("clearCookies")
299300

300301
async def grant_permissions(
301-
self, permissions: List[str], origin: str = None
302+
self, permissions: Sequence[str], origin: str = None
302303
) -> None:
303304
await self._channel.send("grantPermissions", locals_to_params(locals()))
304305

playwright/_impl/_browser_type.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import asyncio
1616
import pathlib
1717
from pathlib import Path
18-
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast
18+
from typing import TYPE_CHECKING, Dict, Optional, Pattern, Sequence, Union, cast
1919

2020
from playwright._impl._api_structures import (
2121
Geolocation,
@@ -72,8 +72,8 @@ async def launch(
7272
self,
7373
executablePath: Union[str, Path] = None,
7474
channel: str = None,
75-
args: List[str] = None,
76-
ignoreDefaultArgs: Union[bool, List[str]] = None,
75+
args: Sequence[str] = None,
76+
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
7777
handleSIGINT: bool = None,
7878
handleSIGTERM: bool = None,
7979
handleSIGHUP: bool = None,
@@ -101,8 +101,8 @@ async def launch_persistent_context(
101101
userDataDir: Union[str, Path],
102102
channel: str = None,
103103
executablePath: Union[str, Path] = None,
104-
args: List[str] = None,
105-
ignoreDefaultArgs: Union[bool, List[str]] = None,
104+
args: Sequence[str] = None,
105+
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
106106
handleSIGINT: bool = None,
107107
handleSIGTERM: bool = None,
108108
handleSIGHUP: bool = None,
@@ -123,7 +123,7 @@ async def launch_persistent_context(
123123
locale: str = None,
124124
timezoneId: str = None,
125125
geolocation: Geolocation = None,
126-
permissions: List[str] = None,
126+
permissions: Sequence[str] = None,
127127
extraHTTPHeaders: Dict[str, str] = None,
128128
offline: bool = None,
129129
httpCredentials: HttpCredentials = None,

playwright/_impl/_connection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
import collections.abc
1617
import contextvars
1718
import datetime
1819
import inspect
@@ -455,7 +456,9 @@ def _replace_channels_with_guids(
455456
return payload
456457
if isinstance(payload, Path):
457458
return str(payload)
458-
if isinstance(payload, list):
459+
if isinstance(payload, collections.abc.Sequence) and not isinstance(
460+
payload, str
461+
):
459462
return list(map(self._replace_channels_with_guids, payload))
460463
if isinstance(payload, Channel):
461464
return dict(guid=payload._guid)

0 commit comments

Comments
 (0)