Skip to content

Commit

Permalink
chore: use Sequence for input List like types (#2178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Nov 30, 2023
1 parent 1bf55d7 commit fa71145
Show file tree
Hide file tree
Showing 22 changed files with 481 additions and 407 deletions.
4 changes: 2 additions & 2 deletions playwright/_impl/_api_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import sys
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Sequence, Union

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

class FrameExpectOptions(TypedDict, total=False):
expressionArg: Any
expectedText: Optional[List[ExpectedTextValue]]
expectedText: Optional[Sequence[ExpectedTextValue]]
expectedNumber: Optional[float]
expectedValue: Optional[Any]
useInnerText: Optional[bool]
Expand Down
65 changes: 39 additions & 26 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, List, Optional, Pattern, Union
import collections.abc
from typing import Any, List, Optional, Pattern, Sequence, Union
from urllib.parse import urljoin

from playwright._impl._api_structures import ExpectedTextValue, FrameExpectOptions
Expand Down Expand Up @@ -149,9 +150,9 @@ def _not(self) -> "LocatorAssertions":
async def to_contain_text(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
Expand All @@ -160,7 +161,9 @@ async def to_contain_text(
ignore_case: bool = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, list):
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(
expected,
match_substring=True,
Expand Down Expand Up @@ -198,9 +201,9 @@ async def to_contain_text(
async def not_to_contain_text(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
Expand Down Expand Up @@ -244,16 +247,18 @@ async def not_to_have_attribute(
async def to_have_class(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, list):
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(expected)
await self._expect_impl(
"to.have.class.array",
Expand All @@ -273,9 +278,9 @@ async def to_have_class(
async def not_to_have_class(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
Expand Down Expand Up @@ -402,7 +407,9 @@ async def not_to_have_value(

async def to_have_values(
self,
values: Union[List[str], List[Pattern[str]], List[Union[Pattern[str], str]]],
values: Union[
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
Expand All @@ -416,7 +423,9 @@ async def to_have_values(

async def not_to_have_values(
self,
values: Union[List[str], List[Pattern[str]], List[Union[Pattern[str], str]]],
values: Union[
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
Expand All @@ -425,9 +434,9 @@ async def not_to_have_values(
async def to_have_text(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
Expand All @@ -436,7 +445,9 @@ async def to_have_text(
ignore_case: bool = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, list):
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(
expected,
normalize_white_space=True,
Expand Down Expand Up @@ -470,9 +481,9 @@ async def to_have_text(
async def not_to_have_text(
self,
expected: Union[
List[str],
List[Pattern[str]],
List[Union[Pattern[str], str]],
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
Expand Down Expand Up @@ -758,11 +769,13 @@ def expected_regex(


def to_expected_text_values(
items: Union[List[Pattern[str]], List[str], List[Union[str, Pattern[str]]]],
items: Union[
Sequence[Pattern[str]], Sequence[str], Sequence[Union[str, Pattern[str]]]
],
match_substring: bool = False,
normalize_white_space: bool = False,
ignore_case: Optional[bool] = None,
) -> List[ExpectedTextValue]:
) -> Sequence[ExpectedTextValue]:
out: List[ExpectedTextValue] = []
assert isinstance(items, list)
for item in items:
Expand Down
8 changes: 4 additions & 4 deletions playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import json
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Sequence, Union, cast

from playwright._impl._api_structures import (
Geolocation,
Expand Down Expand Up @@ -96,7 +96,7 @@ async def new_context(
locale: str = None,
timezoneId: str = None,
geolocation: Geolocation = None,
permissions: List[str] = None,
permissions: Sequence[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: HttpCredentials = None,
Expand Down Expand Up @@ -141,7 +141,7 @@ async def new_page(
locale: str = None,
timezoneId: str = None,
geolocation: Geolocation = None,
permissions: List[str] = None,
permissions: Sequence[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: HttpCredentials = None,
Expand Down Expand Up @@ -200,7 +200,7 @@ async def start_tracing(
page: Page = None,
path: Union[str, Path] = None,
screenshots: bool = None,
categories: List[str] = None,
categories: Sequence[str] = None,
) -> None:
params = locals_to_params(locals())
if page:
Expand Down
9 changes: 5 additions & 4 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
List,
Optional,
Pattern,
Sequence,
Set,
Union,
cast,
Expand Down Expand Up @@ -284,21 +285,21 @@ async def new_page(self) -> Page:
raise Error("Please use browser.new_context()")
return from_channel(await self._channel.send("newPage"))

async def cookies(self, urls: Union[str, List[str]] = None) -> List[Cookie]:
async def cookies(self, urls: Union[str, Sequence[str]] = None) -> List[Cookie]:
if urls is None:
urls = []
if not isinstance(urls, list):
if isinstance(urls, str):
urls = [urls]
return await self._channel.send("cookies", dict(urls=urls))

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

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

async def grant_permissions(
self, permissions: List[str], origin: str = None
self, permissions: Sequence[str], origin: str = None
) -> None:
await self._channel.send("grantPermissions", locals_to_params(locals()))

Expand Down
12 changes: 6 additions & 6 deletions playwright/_impl/_browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import asyncio
import pathlib
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast
from typing import TYPE_CHECKING, Dict, Optional, Pattern, Sequence, Union, cast

from playwright._impl._api_structures import (
Geolocation,
Expand Down Expand Up @@ -72,8 +72,8 @@ async def launch(
self,
executablePath: Union[str, Path] = None,
channel: str = None,
args: List[str] = None,
ignoreDefaultArgs: Union[bool, List[str]] = None,
args: Sequence[str] = None,
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
handleSIGINT: bool = None,
handleSIGTERM: bool = None,
handleSIGHUP: bool = None,
Expand Down Expand Up @@ -101,8 +101,8 @@ async def launch_persistent_context(
userDataDir: Union[str, Path],
channel: str = None,
executablePath: Union[str, Path] = None,
args: List[str] = None,
ignoreDefaultArgs: Union[bool, List[str]] = None,
args: Sequence[str] = None,
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
handleSIGINT: bool = None,
handleSIGTERM: bool = None,
handleSIGHUP: bool = None,
Expand All @@ -123,7 +123,7 @@ async def launch_persistent_context(
locale: str = None,
timezoneId: str = None,
geolocation: Geolocation = None,
permissions: List[str] = None,
permissions: Sequence[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: HttpCredentials = None,
Expand Down
5 changes: 4 additions & 1 deletion playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
import collections.abc
import contextvars
import datetime
import inspect
Expand Down Expand Up @@ -455,7 +456,9 @@ def _replace_channels_with_guids(
return payload
if isinstance(payload, Path):
return str(payload)
if isinstance(payload, list):
if isinstance(payload, collections.abc.Sequence) and not isinstance(
payload, str
):
return list(map(self._replace_channels_with_guids, payload))
if isinstance(payload, Channel):
return dict(guid=payload._guid)
Expand Down
Loading

0 comments on commit fa71145

Please sign in to comment.