Skip to content

Commit 02feb50

Browse files
[typing] Use the re namespace in 're' typing everywhere
1 parent 3def6df commit 02feb50

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

src/_pytest/_code/code.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import os
1515
from pathlib import Path
1616
import re
17-
from re import Pattern
1817
import sys
1918
import traceback
2019
from traceback import format_exception_only
@@ -705,7 +704,7 @@ def _stringify_exception(self, exc: BaseException) -> str:
705704
]
706705
)
707706

708-
def match(self, regexp: str | Pattern[str]) -> Literal[True]:
707+
def match(self, regexp: str | re.Pattern[str]) -> Literal[True]:
709708
"""Check whether the regular expression `regexp` matches the string
710709
representation of the exception using :func:`python:re.search`.
711710
@@ -724,7 +723,7 @@ def _group_contains(
724723
self,
725724
exc_group: BaseExceptionGroup[BaseException],
726725
expected_exception: EXCEPTION_OR_MORE,
727-
match: str | Pattern[str] | None,
726+
match: str | re.Pattern[str] | None,
728727
target_depth: int | None = None,
729728
current_depth: int = 1,
730729
) -> bool:
@@ -754,7 +753,7 @@ def group_contains(
754753
self,
755754
expected_exception: EXCEPTION_OR_MORE,
756755
*,
757-
match: str | Pattern[str] | None = None,
756+
match: str | re.Pattern[str] | None = None,
758757
depth: int | None = None,
759758
) -> bool:
760759
"""Check whether a captured exception group contains a matching exception.
@@ -763,7 +762,7 @@ def group_contains(
763762
The expected exception type, or a tuple if one of multiple possible
764763
exception types are expected.
765764
766-
:param str | Pattern[str] | None match:
765+
:param str | re.Pattern[str] | None match:
767766
If specified, a string containing a regular expression,
768767
or a regular expression object, that is tested against the string
769768
representation of the exception and its `PEP-678 <https://peps.python.org/pep-0678/>` `__notes__`

src/_pytest/doctest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import os
1515
from pathlib import Path
1616
import platform
17-
from re import Pattern
17+
import re
1818
import sys
1919
import traceback
2020
import types
@@ -593,7 +593,6 @@ def _from_module(self, module, object):
593593

594594
def _init_checker_class() -> type[doctest.OutputChecker]:
595595
import doctest
596-
import re
597596

598597
class LiteralsOutputChecker(doctest.OutputChecker):
599598
# Based on doctest_nose_plugin.py from the nltk project
@@ -636,7 +635,7 @@ def check_output(self, want: str, got: str, optionflags: int) -> bool:
636635
if not allow_unicode and not allow_bytes and not allow_number:
637636
return False
638637

639-
def remove_prefixes(regex: Pattern[str], txt: str) -> str:
638+
def remove_prefixes(regex: re.Pattern[str], txt: str) -> str:
640639
return re.sub(regex, r"\1\2", txt)
641640

642641
if allow_unicode:

src/_pytest/junitxml.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import os
1818
import platform
1919
import re
20-
from re import Match
2120
import xml.etree.ElementTree as ET
2221

2322
from _pytest import nodes
@@ -48,7 +47,7 @@ def bin_xml_escape(arg: object) -> str:
4847
The idea is to escape visually for the user rather than for XML itself.
4948
"""
5049

51-
def repl(matchobj: Match[str]) -> str:
50+
def repl(matchobj: re.Match[str]) -> str:
5251
i = ord(matchobj.group())
5352
if i <= 0xFF:
5453
return f"#x{i:02X}"

src/_pytest/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import itertools
2121
import os
2222
from pathlib import Path
23-
from re import Pattern
23+
import re
2424
import types
2525
from typing import Any
2626
from typing import final
@@ -973,7 +973,7 @@ def _idval_from_value(self, val: object) -> str | None:
973973
return _ascii_escaped_by_config(val, self.config)
974974
elif val is None or isinstance(val, (float, int, bool, complex)):
975975
return str(val)
976-
elif isinstance(val, Pattern):
976+
elif isinstance(val, re.Pattern):
977977
return ascii_escaped(val.pattern)
978978
elif val is NOTSET:
979979
# Fallback to default. Note that NOTSET is an enum.Enum.

src/_pytest/python_api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from numbers import Complex
1313
import pprint
1414
import re
15-
from re import Pattern
1615
from types import TracebackType
1716
from typing import Any
1817
from typing import cast
@@ -780,7 +779,7 @@ def _as_numpy_array(obj: object) -> ndarray | None:
780779
def raises(
781780
expected_exception: type[E] | tuple[type[E], ...],
782781
*,
783-
match: str | Pattern[str] | None = ...,
782+
match: str | re.Pattern[str] | None = ...,
784783
) -> RaisesContext[E]: ...
785784

786785

@@ -955,7 +954,7 @@ def raises(
955954
message = f"DID NOT RAISE {expected_exception}"
956955

957956
if not args:
958-
match: str | Pattern[str] | None = kwargs.pop("match", None)
957+
match: str | re.Pattern[str] | None = kwargs.pop("match", None)
959958
if kwargs:
960959
msg = "Unexpected keyword arguments passed to pytest.raises: "
961960
msg += ", ".join(sorted(kwargs))
@@ -983,7 +982,7 @@ def __init__(
983982
self,
984983
expected_exception: type[E] | tuple[type[E], ...],
985984
message: str,
986-
match_expr: str | Pattern[str] | None = None,
985+
match_expr: str | re.Pattern[str] | None = None,
987986
) -> None:
988987
self.expected_exception = expected_exception
989988
self.message = message

src/_pytest/recwarn.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from collections.abc import Iterator
99
from pprint import pformat
1010
import re
11-
from re import Pattern
1211
from types import TracebackType
1312
from typing import Any
1413
from typing import final
@@ -44,7 +43,9 @@ def recwarn() -> Generator[WarningsRecorder]:
4443

4544

4645
@overload
47-
def deprecated_call(*, match: str | Pattern[str] | None = ...) -> WarningsRecorder: ...
46+
def deprecated_call(
47+
*, match: str | re.Pattern[str] | None = ...
48+
) -> WarningsRecorder: ...
4849

4950

5051
@overload
@@ -89,7 +90,7 @@ def deprecated_call(
8990
def warns(
9091
expected_warning: type[Warning] | tuple[type[Warning], ...] = ...,
9192
*,
92-
match: str | Pattern[str] | None = ...,
93+
match: str | re.Pattern[str] | None = ...,
9394
) -> WarningsChecker: ...
9495

9596

@@ -105,7 +106,7 @@ def warns(
105106
def warns(
106107
expected_warning: type[Warning] | tuple[type[Warning], ...] = Warning,
107108
*args: Any,
108-
match: str | Pattern[str] | None = None,
109+
match: str | re.Pattern[str] | None = None,
109110
**kwargs: Any,
110111
) -> WarningsChecker | Any:
111112
r"""Assert that code raises a particular class of warning.
@@ -258,7 +259,7 @@ class WarningsChecker(WarningsRecorder):
258259
def __init__(
259260
self,
260261
expected_warning: type[Warning] | tuple[type[Warning], ...] = Warning,
261-
match_expr: str | Pattern[str] | None = None,
262+
match_expr: str | re.Pattern[str] | None = None,
262263
*,
263264
_ispytest: bool = False,
264265
) -> None:

0 commit comments

Comments
 (0)