Skip to content

Commit 809f11e

Browse files
committed
Fix TypeError when importing pytest on Python 3.5.0 and 3.5.1
The typing module on these versions have these issues: - `typing.Pattern` cannot appear in a Union since it is not considered a class. - `@overload` is not supported in runtime. (On the other hand, mypy doesn't support putting it under `if False`, so we need some runtime hack). Refs #5751.
1 parent 8ccc017 commit 809f11e

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

changelog/5751.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed TypeError when importing pytest on Python 3.5.0 and 3.5.1.

src/_pytest/_code/code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from types import TracebackType
99
from typing import Generic
1010
from typing import Optional
11-
from typing import Pattern
11+
from typing import Pattern # noqa: F401 (used in type string)
1212
from typing import Tuple
1313
from typing import TypeVar
1414
from typing import Union
@@ -591,7 +591,7 @@ def getrepr(
591591
)
592592
return fmt.repr_excinfo(self)
593593

594-
def match(self, regexp: Union[str, Pattern]) -> bool:
594+
def match(self, regexp: Union[str, "Pattern"]) -> bool:
595595
"""
596596
Check whether the regular expression 'regexp' is found in the string
597597
representation of the exception using ``re.search``. If it matches

src/_pytest/python_api.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import math
33
import pprint
4+
import sys
45
from collections.abc import Iterable
56
from collections.abc import Mapping
67
from collections.abc import Sized
@@ -14,7 +15,7 @@
1415
from typing import Generic
1516
from typing import Optional
1617
from typing import overload
17-
from typing import Pattern
18+
from typing import Pattern # noqa: F401 (used in type string)
1819
from typing import Tuple
1920
from typing import TypeVar
2021
from typing import Union
@@ -28,6 +29,12 @@
2829
if False: # TYPE_CHECKING
2930
from typing import Type # noqa: F401 (used in type string)
3031

32+
if sys.version_info <= (3, 5, 1):
33+
34+
def overload(f): # noqa: F811
35+
return f
36+
37+
3138
BASE_TYPE = (type, STRING_TYPES)
3239

3340

@@ -547,12 +554,12 @@ def _is_numpy_array(obj):
547554
def raises(
548555
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
549556
*,
550-
match: Optional[Union[str, Pattern]] = ...
557+
match: Optional[Union[str, "Pattern"]] = ...
551558
) -> "RaisesContext[_E]":
552559
... # pragma: no cover
553560

554561

555-
@overload
562+
@overload # noqa: F811
556563
def raises(
557564
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
558565
func: Callable,
@@ -563,10 +570,10 @@ def raises(
563570
... # pragma: no cover
564571

565572

566-
def raises(
573+
def raises( # noqa: F811
567574
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
568575
*args: Any,
569-
match: Optional[Union[str, Pattern]] = None,
576+
match: Optional[Union[str, "Pattern"]] = None,
570577
**kwargs: Any
571578
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:
572579
r"""
@@ -724,7 +731,7 @@ def __init__(
724731
self,
725732
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
726733
message: str,
727-
match_expr: Optional[Union[str, Pattern]] = None,
734+
match_expr: Optional[Union[str, "Pattern"]] = None,
728735
) -> None:
729736
self.expected_exception = expected_exception
730737
self.message = message

src/_pytest/recwarn.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" recording warnings during test function execution. """
22
import re
3+
import sys
34
import warnings
45
from types import TracebackType
56
from typing import Any
@@ -8,7 +9,7 @@
89
from typing import List
910
from typing import Optional
1011
from typing import overload
11-
from typing import Pattern
12+
from typing import Pattern # noqa: F401 (used in type string)
1213
from typing import Tuple
1314
from typing import Union
1415

@@ -18,6 +19,11 @@
1819
if False: # TYPE_CHECKING
1920
from typing import Type
2021

22+
if sys.version_info <= (3, 5, 1):
23+
24+
def overload(f): # noqa: F811
25+
return f
26+
2127

2228
@yield_fixture
2329
def recwarn():
@@ -58,26 +64,26 @@ def deprecated_call(func=None, *args, **kwargs):
5864
def warns(
5965
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
6066
*,
61-
match: Optional[Union[str, Pattern]] = ...
67+
match: Optional[Union[str, "Pattern"]] = ...
6268
) -> "WarningsChecker":
6369
... # pragma: no cover
6470

6571

66-
@overload
72+
@overload # noqa: F811
6773
def warns(
6874
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
6975
func: Callable,
7076
*args: Any,
71-
match: Optional[Union[str, Pattern]] = ...,
77+
match: Optional[Union[str, "Pattern"]] = ...,
7278
**kwargs: Any
7379
) -> Union[Any]:
7480
... # pragma: no cover
7581

7682

77-
def warns(
83+
def warns( # noqa: F811
7884
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
7985
*args: Any,
80-
match: Optional[Union[str, Pattern]] = None,
86+
match: Optional[Union[str, "Pattern"]] = None,
8187
**kwargs: Any
8288
) -> Union["WarningsChecker", Any]:
8389
r"""Assert that code raises a particular class of warning.
@@ -207,7 +213,7 @@ def __init__(
207213
expected_warning: Optional[
208214
Union["Type[Warning]", Tuple["Type[Warning]", ...]]
209215
] = None,
210-
match_expr: Optional[Union[str, Pattern]] = None,
216+
match_expr: Optional[Union[str, "Pattern"]] = None,
211217
) -> None:
212218
super().__init__()
213219

0 commit comments

Comments
 (0)