Skip to content

Commit c123ce0

Browse files
authored
chore: constrain Pattern type (#1440)
Fixes #1415. NB: Using bytes in patterns with our code will currently explode, so this type constrains to hint to users to use str patterns: ```py await expect(page).to_have_url(re.compile(r".*/grid\.html")) await expect(page).to_have_url(re.compile(rb".*/grid\.html")) ``` This change opts to constrain the type rather than allow for bytes, too.
1 parent 393a060 commit c123ce0

File tree

11 files changed

+243
-203
lines changed

11 files changed

+243
-203
lines changed

playwright/_impl/_assertions.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _not(self) -> "PageAssertions":
6767
return PageAssertions(self._actual_page, not self._is_not)
6868

6969
async def to_have_title(
70-
self, title_or_reg_exp: Union[Pattern, str], timeout: float = None
70+
self, title_or_reg_exp: Union[Pattern[str], str], timeout: float = None
7171
) -> None:
7272
expected_values = to_expected_text_values(
7373
[title_or_reg_exp], normalize_white_space=True
@@ -81,13 +81,13 @@ async def to_have_title(
8181
)
8282

8383
async def not_to_have_title(
84-
self, title_or_reg_exp: Union[Pattern, str], timeout: float = None
84+
self, title_or_reg_exp: Union[Pattern[str], str], timeout: float = None
8585
) -> None:
8686
__tracebackhide__ = True
8787
await self._not.to_have_title(title_or_reg_exp, timeout)
8888

8989
async def to_have_url(
90-
self, url_or_reg_exp: Union[str, Pattern], timeout: float = None
90+
self, url_or_reg_exp: Union[str, Pattern[str]], timeout: float = None
9191
) -> None:
9292
__tracebackhide__ = True
9393
base_url = self._actual_page.context._options.get("baseURL")
@@ -102,7 +102,7 @@ async def to_have_url(
102102
)
103103

104104
async def not_to_have_url(
105-
self, url_or_reg_exp: Union[Pattern, str], timeout: float = None
105+
self, url_or_reg_exp: Union[Pattern[str], str], timeout: float = None
106106
) -> None:
107107
__tracebackhide__ = True
108108
await self._not.to_have_url(url_or_reg_exp, timeout)
@@ -119,7 +119,7 @@ def _not(self) -> "LocatorAssertions":
119119

120120
async def to_contain_text(
121121
self,
122-
expected: Union[List[Union[Pattern, str]], Pattern, str],
122+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
123123
use_inner_text: bool = None,
124124
timeout: float = None,
125125
ignore_case: bool = None,
@@ -162,7 +162,7 @@ async def to_contain_text(
162162

163163
async def not_to_contain_text(
164164
self,
165-
expected: Union[List[Union[Pattern, str]], Pattern, str],
165+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
166166
use_inner_text: bool = None,
167167
timeout: float = None,
168168
ignore_case: bool = None,
@@ -173,7 +173,7 @@ async def not_to_contain_text(
173173
async def to_have_attribute(
174174
self,
175175
name: str,
176-
value: Union[str, Pattern],
176+
value: Union[str, Pattern[str]],
177177
timeout: float = None,
178178
) -> None:
179179
__tracebackhide__ = True
@@ -190,15 +190,15 @@ async def to_have_attribute(
190190
async def not_to_have_attribute(
191191
self,
192192
name: str,
193-
value: Union[str, Pattern],
193+
value: Union[str, Pattern[str]],
194194
timeout: float = None,
195195
) -> None:
196196
__tracebackhide__ = True
197197
await self._not.to_have_attribute(name, value, timeout)
198198

199199
async def to_have_class(
200200
self,
201-
expected: Union[List[Union[Pattern, str]], Pattern, str],
201+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
202202
timeout: float = None,
203203
) -> None:
204204
__tracebackhide__ = True
@@ -221,7 +221,7 @@ async def to_have_class(
221221

222222
async def not_to_have_class(
223223
self,
224-
expected: Union[List[Union[Pattern, str]], Pattern, str],
224+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
225225
timeout: float = None,
226226
) -> None:
227227
__tracebackhide__ = True
@@ -251,7 +251,7 @@ async def not_to_have_count(
251251
async def to_have_css(
252252
self,
253253
name: str,
254-
value: Union[str, Pattern],
254+
value: Union[str, Pattern[str]],
255255
timeout: float = None,
256256
) -> None:
257257
__tracebackhide__ = True
@@ -268,15 +268,15 @@ async def to_have_css(
268268
async def not_to_have_css(
269269
self,
270270
name: str,
271-
value: Union[str, Pattern],
271+
value: Union[str, Pattern[str]],
272272
timeout: float = None,
273273
) -> None:
274274
__tracebackhide__ = True
275275
await self._not.to_have_css(name, value, timeout)
276276

277277
async def to_have_id(
278278
self,
279-
id: Union[str, Pattern],
279+
id: Union[str, Pattern[str]],
280280
timeout: float = None,
281281
) -> None:
282282
__tracebackhide__ = True
@@ -290,7 +290,7 @@ async def to_have_id(
290290

291291
async def not_to_have_id(
292292
self,
293-
id: Union[str, Pattern],
293+
id: Union[str, Pattern[str]],
294294
timeout: float = None,
295295
) -> None:
296296
__tracebackhide__ = True
@@ -323,7 +323,7 @@ async def not_to_have_js_property(
323323

324324
async def to_have_value(
325325
self,
326-
value: Union[str, Pattern],
326+
value: Union[str, Pattern[str]],
327327
timeout: float = None,
328328
) -> None:
329329
__tracebackhide__ = True
@@ -337,15 +337,15 @@ async def to_have_value(
337337

338338
async def not_to_have_value(
339339
self,
340-
value: Union[str, Pattern],
340+
value: Union[str, Pattern[str]],
341341
timeout: float = None,
342342
) -> None:
343343
__tracebackhide__ = True
344344
await self._not.to_have_value(value, timeout)
345345

346346
async def to_have_values(
347347
self,
348-
values: List[Union[Pattern, str]],
348+
values: List[Union[Pattern[str], str]],
349349
timeout: float = None,
350350
) -> None:
351351
__tracebackhide__ = True
@@ -359,15 +359,15 @@ async def to_have_values(
359359

360360
async def not_to_have_values(
361361
self,
362-
values: List[Union[Pattern, str]],
362+
values: List[Union[Pattern[str], str]],
363363
timeout: float = None,
364364
) -> None:
365365
__tracebackhide__ = True
366366
await self._not.to_have_values(values, timeout)
367367

368368
async def to_have_text(
369369
self,
370-
expected: Union[List[Union[Pattern, str]], Pattern, str],
370+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
371371
use_inner_text: bool = None,
372372
timeout: float = None,
373373
ignore_case: bool = None,
@@ -406,7 +406,7 @@ async def to_have_text(
406406

407407
async def not_to_have_text(
408408
self,
409-
expected: Union[List[Union[Pattern, str]], Pattern, str],
409+
expected: Union[List[Union[Pattern[str], str]], Pattern[str], str],
410410
use_inner_text: bool = None,
411411
timeout: float = None,
412412
ignore_case: bool = None,
@@ -602,7 +602,7 @@ async def not_to_be_ok(self) -> None:
602602

603603

604604
def expected_regex(
605-
pattern: Pattern,
605+
pattern: Pattern[str],
606606
match_substring: bool,
607607
normalize_white_space: bool,
608608
ignore_case: Optional[bool] = None,
@@ -620,7 +620,7 @@ def expected_regex(
620620

621621

622622
def to_expected_text_values(
623-
items: Union[List[Pattern], List[str], List[Union[str, Pattern]]],
623+
items: Union[List[Pattern[str]], List[str], List[Union[str, Pattern[str]]]],
624624
match_substring: bool = False,
625625
normalize_white_space: bool = False,
626626
ignore_case: Optional[bool] = None,

playwright/_impl/_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def new_context(
117117
baseURL: str = None,
118118
strictSelectors: bool = None,
119119
serviceWorkers: ServiceWorkersPolicy = None,
120-
recordHarUrlFilter: Union[Pattern, str] = None,
120+
recordHarUrlFilter: Union[Pattern[str], str] = None,
121121
recordHarMode: HarMode = None,
122122
recordHarContent: HarContentPolicy = None,
123123
) -> BrowserContext:
@@ -165,7 +165,7 @@ async def new_page(
165165
baseURL: str = None,
166166
strictSelectors: bool = None,
167167
serviceWorkers: ServiceWorkersPolicy = None,
168-
recordHarUrlFilter: Union[Pattern, str] = None,
168+
recordHarUrlFilter: Union[Pattern[str], str] = None,
169169
recordHarMode: HarMode = None,
170170
recordHarContent: HarContentPolicy = None,
171171
) -> Page:

playwright/_impl/_browser_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ async def _record_into_har(
321321
self,
322322
har: Union[Path, str],
323323
page: Optional[Page] = None,
324-
url: Union[Pattern, str] = None,
324+
url: Union[Pattern[str], str] = None,
325325
) -> None:
326326
params = {
327327
"options": prepare_record_har_options(
@@ -341,7 +341,7 @@ async def _record_into_har(
341341
async def route_from_har(
342342
self,
343343
har: Union[Path, str],
344-
url: Union[Pattern, str] = None,
344+
url: Union[Pattern[str], str] = None,
345345
not_found: RouteFromHarNotFoundPolicy = None,
346346
update: bool = None,
347347
) -> None:

playwright/_impl/_browser_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ async def launch_persistent_context(
142142
baseURL: str = None,
143143
strictSelectors: bool = None,
144144
serviceWorkers: ServiceWorkersPolicy = None,
145-
recordHarUrlFilter: Union[Pattern, str] = None,
145+
recordHarUrlFilter: Union[Pattern[str], str] = None,
146146
recordHarMode: HarMode = None,
147147
recordHarContent: HarContentPolicy = None,
148148
) -> BrowserContext:

playwright/_impl/_frame.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ async def fill(
513513
await self._channel.send("fill", locals_to_params(locals()))
514514

515515
def locator(
516-
self, selector: str, has_text: Union[str, Pattern] = None, has: Locator = None
516+
self,
517+
selector: str,
518+
has_text: Union[str, Pattern[str]] = None,
519+
has: Locator = None,
517520
) -> Locator:
518521
return Locator(self, selector, has_text=has_text, has=has)
519522

playwright/_impl/_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
from playwright._impl._api_structures import HeadersArray
5454
from playwright._impl._network import Request, Response, Route
5555

56-
URLMatch = Union[str, Pattern, Callable[[str], bool]]
57-
URLMatchRequest = Union[str, Pattern, Callable[["Request"], bool]]
58-
URLMatchResponse = Union[str, Pattern, Callable[["Response"], bool]]
56+
URLMatch = Union[str, Pattern[str], Callable[[str], bool]]
57+
URLMatchRequest = Union[str, Pattern[str], Callable[["Request"], bool]]
58+
URLMatchResponse = Union[str, Pattern[str], Callable[["Response"], bool]]
5959
RouteHandlerCallback = Union[
6060
Callable[["Route"], Any], Callable[["Route", "Request"], Any]
6161
]
@@ -152,7 +152,7 @@ class FrameNavigatedEvent(TypedDict):
152152
class URLMatcher:
153153
def __init__(self, base_url: Union[str, None], match: URLMatch) -> None:
154154
self._callback: Optional[Callable[[str], bool]] = None
155-
self._regex_obj: Optional[Pattern] = None
155+
self._regex_obj: Optional[Pattern[str]] = None
156156
if isinstance(match, str):
157157
if base_url and not match.startswith("*"):
158158
match = urljoin(base_url, match)

playwright/_impl/_locator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
self,
6565
frame: "Frame",
6666
selector: str,
67-
has_text: Union[str, Pattern] = None,
67+
has_text: Union[str, Pattern[str]] = None,
6868
has: "Locator" = None,
6969
) -> None:
7070
self._frame = frame
@@ -197,7 +197,7 @@ async def fill(
197197
def locator(
198198
self,
199199
selector: str,
200-
has_text: Union[str, Pattern] = None,
200+
has_text: Union[str, Pattern[str]] = None,
201201
has: "Locator" = None,
202202
) -> "Locator":
203203
return Locator(
@@ -237,7 +237,7 @@ def nth(self, index: int) -> "Locator":
237237

238238
def filter(
239239
self,
240-
has_text: Union[str, Pattern] = None,
240+
has_text: Union[str, Pattern[str]] = None,
241241
has: "Locator" = None,
242242
) -> "Locator":
243243
return Locator(
@@ -576,7 +576,10 @@ def __init__(self, frame: "Frame", frame_selector: str) -> None:
576576
self._frame_selector = frame_selector
577577

578578
def locator(
579-
self, selector: str, has_text: Union[str, Pattern] = None, has: "Locator" = None
579+
self,
580+
selector: str,
581+
has_text: Union[str, Pattern[str]] = None,
582+
has: "Locator" = None,
580583
) -> Locator:
581584
return Locator(
582585
self._frame,

playwright/_impl/_page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ async def unroute(
605605
async def route_from_har(
606606
self,
607607
har: Union[Path, str],
608-
url: Union[Pattern, str] = None,
608+
url: Union[Pattern[str], str] = None,
609609
not_found: RouteFromHarNotFoundPolicy = None,
610610
update: bool = None,
611611
) -> None:
@@ -732,7 +732,7 @@ async def fill(
732732
def locator(
733733
self,
734734
selector: str,
735-
has_text: Union[str, Pattern] = None,
735+
has_text: Union[str, Pattern[str]] = None,
736736
has: "Locator" = None,
737737
) -> "Locator":
738738
return self._main_frame.locator(selector, has_text=has_text, has=has)

0 commit comments

Comments
 (0)