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

File tree

18 files changed

+44
-17
lines changed

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

0 commit comments

Comments
 (0)