Skip to content

Commit 12fc00c

Browse files
committed
Use TYPE_CHECKING instead of False
This allows for e.g. Jedi to infer types (it checks the name). It was only used to support Python 3.5.0/3.5.1, where this is is not available in the `typing` module. Ref: davidhalter/jedi#1472 Put at the top to work around circular import.
1 parent f2659f7 commit 12fc00c

18 files changed

+44
-17
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ exclude_lines =
2424
\#\s*pragma: no cover
2525
^\s*raise NotImplementedError\b
2626
^\s*return NotImplemented\b
27+
28+
^\s*if TYPE_CHECKING:

changelog/6435.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``typing_extensions`` as dependency on Python 3.5.1 or lower.

doc/en/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import sys
2020

2121
from _pytest import __version__ as version
22+
from _pytest.compat import TYPE_CHECKING
2223

23-
if False: # TYPE_CHECKING
24+
if TYPE_CHECKING:
2425
import sphinx.application
2526

2627

src/_pytest/_code/code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
from _pytest._io.saferepr import safeformat
3333
from _pytest._io.saferepr import saferepr
3434
from _pytest.compat import overload
35+
from _pytest.compat import TYPE_CHECKING
3536

36-
if False: # TYPE_CHECKING
37+
if TYPE_CHECKING:
3738
from typing import Type
3839
from typing_extensions import Literal
3940
from weakref import ReferenceType # noqa: F401

src/_pytest/compat.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
"""
22
python version compatibility code
33
"""
4+
import sys # isort:skip
5+
6+
if sys.version_info < (3, 5, 2):
7+
TYPE_CHECKING = False # type: bool
8+
else:
9+
from typing import TYPE_CHECKING
10+
411
import functools
512
import inspect
613
import io
714
import os
815
import re
9-
import sys
1016
from contextlib import contextmanager
1117
from inspect import Parameter
1218
from inspect import signature
@@ -28,7 +34,8 @@
2834
from _pytest.outcomes import fail
2935
from _pytest.outcomes import TEST_OUTCOME
3036

31-
if False: # TYPE_CHECKING
37+
38+
if TYPE_CHECKING:
3239
from typing import Type # noqa: F401 (used in type string)
3340

3441

src/_pytest/config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@
3737
from _pytest._code import ExceptionInfo
3838
from _pytest._code import filter_traceback
3939
from _pytest.compat import importlib_metadata
40+
from _pytest.compat import TYPE_CHECKING
4041
from _pytest.outcomes import fail
4142
from _pytest.outcomes import Skipped
4243
from _pytest.pathlib import Path
4344
from _pytest.warning_types import PytestConfigWarning
4445

45-
if False: # TYPE_CHECKING
46+
if TYPE_CHECKING:
4647
from typing import Type
4748

4849
from .argparsing import Argument

src/_pytest/config/argparsing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
import py
1717

18+
from _pytest.compat import TYPE_CHECKING
1819
from _pytest.config.exceptions import UsageError
1920

20-
if False: # TYPE_CHECKING
21+
if TYPE_CHECKING:
2122
from typing import NoReturn
2223
from typing_extensions import Literal # noqa: F401
2324

src/_pytest/config/findpaths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import py
66

77
from .exceptions import UsageError
8+
from _pytest.compat import TYPE_CHECKING
89
from _pytest.outcomes import fail
910

10-
if False:
11+
if TYPE_CHECKING:
1112
from . import Config # noqa: F401
1213

1314

src/_pytest/doctest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
from _pytest._code.code import ReprFileLocation
2020
from _pytest._code.code import TerminalRepr
2121
from _pytest.compat import safe_getattr
22+
from _pytest.compat import TYPE_CHECKING
2223
from _pytest.fixtures import FixtureRequest
2324
from _pytest.outcomes import Skipped
2425
from _pytest.python_api import approx
2526
from _pytest.warning_types import PytestWarning
2627

27-
if False: # TYPE_CHECKING
28+
if TYPE_CHECKING:
2829
import doctest
2930
from typing import Type
3031

src/_pytest/fixtures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
from _pytest.compat import is_generator
2828
from _pytest.compat import NOTSET
2929
from _pytest.compat import safe_getattr
30+
from _pytest.compat import TYPE_CHECKING
3031
from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS
3132
from _pytest.deprecated import FUNCARGNAMES
3233
from _pytest.outcomes import fail
3334
from _pytest.outcomes import TEST_OUTCOME
3435

35-
if False: # TYPE_CHECKING
36+
if TYPE_CHECKING:
3637
from typing import Type
3738

3839
from _pytest import nodes

src/_pytest/nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from _pytest._code.code import ReprExceptionInfo
1818
from _pytest.compat import cached_property
1919
from _pytest.compat import getfslineno
20+
from _pytest.compat import TYPE_CHECKING
2021
from _pytest.config import Config
2122
from _pytest.deprecated import NODE_USE_FROM_PARENT
2223
from _pytest.fixtures import FixtureDef
@@ -27,7 +28,7 @@
2728
from _pytest.mark.structures import NodeKeywords
2829
from _pytest.outcomes import Failed
2930

30-
if False: # TYPE_CHECKING
31+
if TYPE_CHECKING:
3132
# Imported here due to circular import.
3233
from _pytest.main import Session # noqa: F401
3334

src/_pytest/outcomes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
from packaging.version import Version
1010

11-
if False: # TYPE_CHECKING
11+
from _pytest.compat import TYPE_CHECKING
12+
13+
if TYPE_CHECKING:
1214
from typing import NoReturn
1315

1416

src/_pytest/pytester.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
from _pytest._io.saferepr import saferepr
2929
from _pytest.capture import MultiCapture
3030
from _pytest.capture import SysCapture
31+
from _pytest.compat import TYPE_CHECKING
3132
from _pytest.fixtures import FixtureRequest
3233
from _pytest.main import ExitCode
3334
from _pytest.main import Session
3435
from _pytest.monkeypatch import MonkeyPatch
3536
from _pytest.pathlib import Path
3637
from _pytest.reports import TestReport
3738

38-
if False: # TYPE_CHECKING
39+
if TYPE_CHECKING:
3940
from typing import Type
4041

4142

@@ -189,7 +190,7 @@ def __repr__(self):
189190
del d["_name"]
190191
return "<ParsedCall {!r}(**{!r})>".format(self._name, d)
191192

192-
if False: # TYPE_CHECKING
193+
if TYPE_CHECKING:
193194
# The class has undetermined attributes, this tells mypy about it.
194195
def __getattr__(self, key):
195196
raise NotImplementedError()

src/_pytest/python_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import _pytest._code
2424
from _pytest.compat import overload
2525
from _pytest.compat import STRING_TYPES
26+
from _pytest.compat import TYPE_CHECKING
2627
from _pytest.outcomes import fail
2728

28-
if False: # TYPE_CHECKING
29+
if TYPE_CHECKING:
2930
from typing import Type # noqa: F401 (used in type string)
3031

3132

src/_pytest/recwarn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from typing import Union
1313

1414
from _pytest.compat import overload
15+
from _pytest.compat import TYPE_CHECKING
1516
from _pytest.fixtures import yield_fixture
1617
from _pytest.outcomes import fail
1718

18-
if False: # TYPE_CHECKING
19+
if TYPE_CHECKING:
1920
from typing import Type
2021

2122

src/_pytest/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
from .reports import CollectReport
1616
from .reports import TestReport
1717
from _pytest._code.code import ExceptionInfo
18+
from _pytest.compat import TYPE_CHECKING
1819
from _pytest.nodes import Node
1920
from _pytest.outcomes import Exit
2021
from _pytest.outcomes import Skipped
2122
from _pytest.outcomes import TEST_OUTCOME
2223

23-
if False: # TYPE_CHECKING
24+
if TYPE_CHECKING:
2425
from typing import Type
2526

2627
#

src/_pytest/warning_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import attr
66

7+
from _pytest.compat import TYPE_CHECKING
78

8-
if False: # TYPE_CHECKING
9+
if TYPE_CHECKING:
910
from typing import Type # noqa: F401 (used in type string)
1011

1112

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ markers =
182182
[flake8]
183183
max-line-length = 120
184184
extend-ignore = E203
185+
per-file-ignores =
186+
src/_pytest/compat.py: E402
185187

186188
[isort]
187189
; This config mimics what reorder-python-imports does.

0 commit comments

Comments
 (0)