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.
This commit is contained in:
Ran Benita 2019-08-16 10:08:18 +03:00
parent 8ccc0177c8
commit 9f3bfe82cf
4 changed files with 26 additions and 12 deletions

View File

@ -0,0 +1 @@
Fixed ``TypeError`` when importing pytest on Python 3.5.0 and 3.5.1.

View File

@ -591,7 +591,7 @@ class ExceptionInfo(Generic[_E]):
) )
return fmt.repr_excinfo(self) 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 Check whether the regular expression 'regexp' is found in the string
representation of the exception using ``re.search``. If it matches representation of the exception using ``re.search``. If it matches

View File

@ -1,6 +1,7 @@
import inspect import inspect
import math import math
import pprint import pprint
import sys
from collections.abc import Iterable from collections.abc import Iterable
from collections.abc import Mapping from collections.abc import Mapping
from collections.abc import Sized from collections.abc import Sized
@ -28,6 +29,12 @@ from _pytest.outcomes import fail
if False: # TYPE_CHECKING if False: # TYPE_CHECKING
from typing import Type # noqa: F401 (used in type string) 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) BASE_TYPE = (type, STRING_TYPES)
@ -547,12 +554,12 @@ _E = TypeVar("_E", bound=BaseException)
def raises( def raises(
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*, *,
match: Optional[Union[str, Pattern]] = ... match: "Optional[Union[str, Pattern]]" = ...
) -> "RaisesContext[_E]": ) -> "RaisesContext[_E]":
... # pragma: no cover ... # pragma: no cover
@overload @overload # noqa: F811
def raises( def raises(
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
func: Callable, func: Callable,
@ -563,10 +570,10 @@ def raises(
... # pragma: no cover ... # pragma: no cover
def raises( def raises( # noqa: F811
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*args: Any, *args: Any,
match: Optional[Union[str, Pattern]] = None, match: Optional[Union[str, "Pattern"]] = None,
**kwargs: Any **kwargs: Any
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]: ) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:
r""" r"""
@ -724,7 +731,7 @@ class RaisesContext(Generic[_E]):
self, self,
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
message: str, message: str,
match_expr: Optional[Union[str, Pattern]] = None, match_expr: Optional[Union[str, "Pattern"]] = None,
) -> None: ) -> None:
self.expected_exception = expected_exception self.expected_exception = expected_exception
self.message = message self.message = message

View File

@ -1,5 +1,6 @@
""" recording warnings during test function execution. """ """ recording warnings during test function execution. """
import re import re
import sys
import warnings import warnings
from types import TracebackType from types import TracebackType
from typing import Any from typing import Any
@ -18,6 +19,11 @@ from _pytest.outcomes import fail
if False: # TYPE_CHECKING if False: # TYPE_CHECKING
from typing import Type from typing import Type
if sys.version_info <= (3, 5, 1):
def overload(f): # noqa: F811
return f
@yield_fixture @yield_fixture
def recwarn(): def recwarn():
@ -58,26 +64,26 @@ def deprecated_call(func=None, *args, **kwargs):
def warns( def warns(
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]], expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
*, *,
match: Optional[Union[str, Pattern]] = ... match: "Optional[Union[str, Pattern]]" = ...
) -> "WarningsChecker": ) -> "WarningsChecker":
... # pragma: no cover ... # pragma: no cover
@overload @overload # noqa: F811
def warns( def warns(
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]], expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
func: Callable, func: Callable,
*args: Any, *args: Any,
match: Optional[Union[str, Pattern]] = ..., match: Optional[Union[str, "Pattern"]] = ...,
**kwargs: Any **kwargs: Any
) -> Union[Any]: ) -> Union[Any]:
... # pragma: no cover ... # pragma: no cover
def warns( def warns( # noqa: F811
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]], expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
*args: Any, *args: Any,
match: Optional[Union[str, Pattern]] = None, match: Optional[Union[str, "Pattern"]] = None,
**kwargs: Any **kwargs: Any
) -> Union["WarningsChecker", Any]: ) -> Union["WarningsChecker", Any]:
r"""Assert that code raises a particular class of warning. r"""Assert that code raises a particular class of warning.
@ -207,7 +213,7 @@ class WarningsChecker(WarningsRecorder):
expected_warning: Optional[ expected_warning: Optional[
Union["Type[Warning]", Tuple["Type[Warning]", ...]] Union["Type[Warning]", Tuple["Type[Warning]", ...]]
] = None, ] = None,
match_expr: Optional[Union[str, Pattern]] = None, match_expr: Optional[Union[str, "Pattern"]] = None,
) -> None: ) -> None:
super().__init__() super().__init__()