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)
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

View File

@ -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

View File

@ -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__()