Skip to content

Commit

Permalink
Fix TypeError when importing pytest on Python 3.5.0 and 3.5.1
Browse files Browse the repository at this point in the history
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 pytest-dev#5751.
  • Loading branch information
bluetech committed Aug 16, 2019
1 parent 8ccc017 commit 809f11e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/5751.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed TypeError when importing pytest on Python 3.5.0 and 3.5.1.
4 changes: 2 additions & 2 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from types import TracebackType
from typing import Generic
from typing import Optional
from typing import Pattern
from typing import Pattern # noqa: F401 (used in type string)
from typing import Tuple
from typing import TypeVar
from typing import Union
Expand Down Expand Up @@ -591,7 +591,7 @@ def getrepr(
)
return fmt.repr_excinfo(self)

def match(self, regexp: Union[str, Pattern]) -> bool:
def match(self, regexp: Union[str, "Pattern"]) -> bool:
"""
Check whether the regular expression 'regexp' is found in the string
representation of the exception using ``re.search``. If it matches
Expand Down
19 changes: 13 additions & 6 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import math
import pprint
import sys
from collections.abc import Iterable
from collections.abc import Mapping
from collections.abc import Sized
Expand All @@ -14,7 +15,7 @@
from typing import Generic
from typing import Optional
from typing import overload
from typing import Pattern
from typing import Pattern # noqa: F401 (used in type string)
from typing import Tuple
from typing import TypeVar
from typing import Union
Expand All @@ -28,6 +29,12 @@
if False: # TYPE_CHECKING
from typing import Type # noqa: F401 (used in type string)

if sys.version_info <= (3, 5, 1):

def overload(f): # noqa: F811
return f


BASE_TYPE = (type, STRING_TYPES)


Expand Down Expand Up @@ -547,12 +554,12 @@ def _is_numpy_array(obj):
def raises(
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*,
match: Optional[Union[str, Pattern]] = ...
match: Optional[Union[str, "Pattern"]] = ...
) -> "RaisesContext[_E]":
... # pragma: no cover


@overload
@overload # noqa: F811
def raises(
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
func: Callable,
Expand All @@ -563,10 +570,10 @@ def raises(
... # pragma: no cover


def raises(
def raises( # noqa: F811
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*args: Any,
match: Optional[Union[str, Pattern]] = None,
match: Optional[Union[str, "Pattern"]] = None,
**kwargs: Any
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:
r"""
Expand Down Expand Up @@ -724,7 +731,7 @@ def __init__(
self,
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
message: str,
match_expr: Optional[Union[str, Pattern]] = None,
match_expr: Optional[Union[str, "Pattern"]] = None,
) -> None:
self.expected_exception = expected_exception
self.message = message
Expand Down
20 changes: 13 additions & 7 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" recording warnings during test function execution. """
import re
import sys
import warnings
from types import TracebackType
from typing import Any
Expand All @@ -8,7 +9,7 @@
from typing import List
from typing import Optional
from typing import overload
from typing import Pattern
from typing import Pattern # noqa: F401 (used in type string)
from typing import Tuple
from typing import Union

Expand All @@ -18,6 +19,11 @@
if False: # TYPE_CHECKING
from typing import Type

if sys.version_info <= (3, 5, 1):

def overload(f): # noqa: F811
return f


@yield_fixture
def recwarn():
Expand Down Expand Up @@ -58,26 +64,26 @@ def deprecated_call(func=None, *args, **kwargs):
def warns(
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
*,
match: Optional[Union[str, Pattern]] = ...
match: Optional[Union[str, "Pattern"]] = ...
) -> "WarningsChecker":
... # pragma: no cover


@overload
@overload # noqa: F811
def warns(
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
func: Callable,
*args: Any,
match: Optional[Union[str, Pattern]] = ...,
match: Optional[Union[str, "Pattern"]] = ...,
**kwargs: Any
) -> Union[Any]:
... # pragma: no cover


def warns(
def warns( # noqa: F811
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
*args: Any,
match: Optional[Union[str, Pattern]] = None,
match: Optional[Union[str, "Pattern"]] = None,
**kwargs: Any
) -> Union["WarningsChecker", Any]:
r"""Assert that code raises a particular class of warning.
Expand Down Expand Up @@ -207,7 +213,7 @@ def __init__(
expected_warning: Optional[
Union["Type[Warning]", Tuple["Type[Warning]", ...]]
] = None,
match_expr: Optional[Union[str, Pattern]] = None,
match_expr: Optional[Union[str, "Pattern"]] = None,
) -> None:
super().__init__()

Expand Down

0 comments on commit 809f11e

Please sign in to comment.