diff --git a/changelog/5751.bugfix.rst b/changelog/5751.bugfix.rst new file mode 100644 index 000000000..879909c8b --- /dev/null +++ b/changelog/5751.bugfix.rst @@ -0,0 +1 @@ +Fixed ``TypeError`` when importing pytest on Python 3.5.0 and 3.5.1. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 7d72234e7..a0f4d15ce 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -591,7 +591,7 @@ class ExceptionInfo(Generic[_E]): ) 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 diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index fbc3d914e..c5e06d491 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -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 @@ -28,6 +29,12 @@ from _pytest.outcomes import fail 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) @@ -547,12 +554,12 @@ _E = TypeVar("_E", bound=BaseException) 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, @@ -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""" @@ -724,7 +731,7 @@ class RaisesContext(Generic[_E]): 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 diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index 19e3938c3..27519fd46 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -1,5 +1,6 @@ """ recording warnings during test function execution. """ import re +import sys import warnings from types import TracebackType from typing import Any @@ -18,6 +19,11 @@ from _pytest.outcomes import fail 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(): @@ -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. @@ -207,7 +213,7 @@ class WarningsChecker(WarningsRecorder): 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__()