Skip to content

Commit daff906

Browse files
authored
Merge pull request #5752 from bluetech/typing-py350-fix
Fix TypeError when importing pytest on Python 3.5.0 and 3.5.1
2 parents 0e569fa + 43eab91 commit daff906

File tree

8 files changed

+34
-24
lines changed

8 files changed

+34
-24
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
hooks:
2727
- id: flake8
2828
language_version: python3
29-
additional_dependencies: [flake8-typing-imports]
29+
additional_dependencies: [flake8-typing-imports==1.3.0]
3030
- repo: https://github.com/asottile/reorder_python_imports
3131
rev: v1.4.0
3232
hooks:

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ jobs:
4343
python: 'pypy3'
4444

4545
- env: TOXENV=py35-xdist
46-
python: '3.5'
46+
dist: trusty
47+
python: '3.5.0'
4748

4849
# Coverage for:
4950
# - pytester's LsofFdLeakChecker

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/compat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from contextlib import contextmanager
1010
from inspect import Parameter
1111
from inspect import signature
12+
from typing import overload
1213

1314
import attr
1415
import py
@@ -347,3 +348,9 @@ def funcargnames(self):
347348

348349
warnings.warn(FUNCARGNAMES, stacklevel=2)
349350
return self.fixturenames
351+
352+
353+
if sys.version_info < (3, 5, 2): # pragma: no cover
354+
355+
def overload(f): # noqa: F811
356+
return f

src/_pytest/python_api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from typing import cast
1414
from typing import Generic
1515
from typing import Optional
16-
from typing import overload
1716
from typing import Pattern
1817
from typing import Tuple
1918
from typing import TypeVar
@@ -22,12 +21,14 @@
2221
from more_itertools.more import always_iterable
2322

2423
import _pytest._code
24+
from _pytest.compat import overload
2525
from _pytest.compat import STRING_TYPES
2626
from _pytest.outcomes import fail
2727

2828
if False: # TYPE_CHECKING
2929
from typing import Type # noqa: F401 (used in type string)
3030

31+
3132
BASE_TYPE = (type, STRING_TYPES)
3233

3334

@@ -547,12 +548,12 @@ def _is_numpy_array(obj):
547548
def raises(
548549
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
549550
*,
550-
match: Optional[Union[str, Pattern]] = ...
551+
match: "Optional[Union[str, Pattern]]" = ...
551552
) -> "RaisesContext[_E]":
552553
... # pragma: no cover
553554

554555

555-
@overload
556+
@overload # noqa: F811
556557
def raises(
557558
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
558559
func: Callable,
@@ -563,10 +564,10 @@ def raises(
563564
... # pragma: no cover
564565

565566

566-
def raises(
567+
def raises( # noqa: F811
567568
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
568569
*args: Any,
569-
match: Optional[Union[str, Pattern]] = None,
570+
match: Optional[Union[str, "Pattern"]] = None,
570571
**kwargs: Any
571572
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:
572573
r"""
@@ -724,7 +725,7 @@ def __init__(
724725
self,
725726
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
726727
message: str,
727-
match_expr: Optional[Union[str, Pattern]] = None,
728+
match_expr: Optional[Union[str, "Pattern"]] = None,
728729
) -> None:
729730
self.expected_exception = expected_exception
730731
self.message = message

src/_pytest/recwarn.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from typing import Iterator
88
from typing import List
99
from typing import Optional
10-
from typing import overload
1110
from typing import Pattern
1211
from typing import Tuple
1312
from typing import Union
1413

14+
from _pytest.compat import overload
1515
from _pytest.fixtures import yield_fixture
1616
from _pytest.outcomes import fail
1717

@@ -58,26 +58,26 @@ def deprecated_call(func=None, *args, **kwargs):
5858
def warns(
5959
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
6060
*,
61-
match: Optional[Union[str, Pattern]] = ...
61+
match: "Optional[Union[str, Pattern]]" = ...
6262
) -> "WarningsChecker":
6363
... # pragma: no cover
6464

6565

66-
@overload
66+
@overload # noqa: F811
6767
def warns(
6868
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
6969
func: Callable,
7070
*args: Any,
71-
match: Optional[Union[str, Pattern]] = ...,
71+
match: Optional[Union[str, "Pattern"]] = ...,
7272
**kwargs: Any
7373
) -> Union[Any]:
7474
... # pragma: no cover
7575

7676

77-
def warns(
77+
def warns( # noqa: F811
7878
expected_warning: Union["Type[Warning]", Tuple["Type[Warning]", ...]],
7979
*args: Any,
80-
match: Optional[Union[str, Pattern]] = None,
80+
match: Optional[Union[str, "Pattern"]] = None,
8181
**kwargs: Any
8282
) -> Union["WarningsChecker", Any]:
8383
r"""Assert that code raises a particular class of warning.
@@ -207,7 +207,7 @@ def __init__(
207207
expected_warning: Optional[
208208
Union["Type[Warning]", Tuple["Type[Warning]", ...]]
209209
] = None,
210-
match_expr: Optional[Union[str, Pattern]] = None,
210+
match_expr: Optional[Union[str, "Pattern"]] = None,
211211
) -> None:
212212
super().__init__()
213213

testing/python/raises.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,16 @@ def test_raises_cyclic_reference(self, method):
163163

164164
class T:
165165
def __call__(self):
166+
# Early versions of Python 3.5 have some bug causing the
167+
# __call__ frame to still refer to t even after everything
168+
# is done. This makes the test pass for them.
169+
if sys.version_info < (3, 5, 2): # pragma: no cover
170+
del self
166171
raise ValueError
167172

168173
t = T()
174+
refcount = len(gc.get_referrers(t))
175+
169176
if method == "function":
170177
pytest.raises(ValueError, t)
171178
else:
@@ -175,14 +182,7 @@ def __call__(self):
175182
# ensure both forms of pytest.raises don't leave exceptions in sys.exc_info()
176183
assert sys.exc_info() == (None, None, None)
177184

178-
del t
179-
# Make sure this does get updated in locals dict
180-
# otherwise it could keep a reference
181-
locals()
182-
183-
# ensure the t instance is not stuck in a cyclic reference
184-
for o in gc.get_objects():
185-
assert type(o) is not T
185+
assert refcount == len(gc.get_referrers(t))
186186

187187
def test_raises_match(self):
188188
msg = r"with base \d+"

0 commit comments

Comments
 (0)