Skip to content

Commit fcb9ca5

Browse files
committed
Use TYPE_CHECKING instead of False
This allows for e.g. Jedi to infer types. It was only used to support Python 3.5.0/3.5.1, where this is available in `typing_extensions`. Ref: davidhalter/jedi#1472
1 parent 2d488f7 commit fcb9ca5

15 files changed

+76
-16
lines changed

doc/en/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
from _pytest import __version__ as version
2222

23-
if False: # TYPE_CHECKING
23+
try:
24+
from typing import TYPE_CHECKING
25+
except ImportError:
26+
from typing_extensions import TYPE_CHECKING
27+
if TYPE_CHECKING:
2428
import sphinx.application
2529

2630

src/_pytest/_code/code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
from _pytest._io.saferepr import saferepr
3434
from _pytest.compat import overload
3535

36-
if False: # TYPE_CHECKING
36+
try:
37+
from typing import TYPE_CHECKING
38+
except ImportError:
39+
from typing_extensions import TYPE_CHECKING
40+
if TYPE_CHECKING:
3741
from typing import Type
3842
from typing_extensions import Literal
3943
from weakref import ReferenceType # noqa: F401

src/_pytest/compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
from _pytest.outcomes import fail
2929
from _pytest.outcomes import TEST_OUTCOME
3030

31-
if False: # TYPE_CHECKING
31+
try:
32+
from typing import TYPE_CHECKING
33+
except ImportError:
34+
from typing_extensions import TYPE_CHECKING
35+
if TYPE_CHECKING:
3236
from typing import Type # noqa: F401 (used in type string)
3337

3438

src/_pytest/config/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
from _pytest.pathlib import Path
4343
from _pytest.warning_types import PytestConfigWarning
4444

45-
if False: # TYPE_CHECKING
45+
try:
46+
from typing import TYPE_CHECKING
47+
except ImportError:
48+
from typing_extensions import TYPE_CHECKING
49+
if TYPE_CHECKING:
4650
from typing import Type
4751

4852
from .argparsing import Argument

src/_pytest/config/argparsing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
from _pytest.config.exceptions import UsageError
1919

20-
if False: # TYPE_CHECKING
20+
try:
21+
from typing import TYPE_CHECKING
22+
except ImportError:
23+
from typing_extensions import TYPE_CHECKING
24+
if TYPE_CHECKING:
2125
from typing import NoReturn
2226
from typing_extensions import Literal # noqa: F401
2327

src/_pytest/config/findpaths.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from .exceptions import UsageError
88
from _pytest.outcomes import fail
99

10-
if False:
10+
try:
11+
from typing import TYPE_CHECKING
12+
except ImportError:
13+
from typing_extensions import TYPE_CHECKING
14+
if TYPE_CHECKING:
1115
from . import Config # noqa: F401
1216

1317

src/_pytest/doctest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
from _pytest.python_api import approx
2525
from _pytest.warning_types import PytestWarning
2626

27-
if False: # TYPE_CHECKING
27+
try:
28+
from typing import TYPE_CHECKING
29+
except ImportError:
30+
from typing_extensions import TYPE_CHECKING
31+
if TYPE_CHECKING:
2832
import doctest
2933
from typing import Type
3034

src/_pytest/fixtures.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
from _pytest.outcomes import fail
3333
from _pytest.outcomes import TEST_OUTCOME
3434

35-
if False: # TYPE_CHECKING
35+
try:
36+
from typing import TYPE_CHECKING
37+
except ImportError:
38+
from typing_extensions import TYPE_CHECKING
39+
if TYPE_CHECKING:
3640
from typing import Type
3741

3842
from _pytest import nodes

src/_pytest/nodes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
from _pytest.mark.structures import NodeKeywords
2828
from _pytest.outcomes import Failed
2929

30-
if False: # TYPE_CHECKING
30+
try:
31+
from typing import TYPE_CHECKING
32+
except ImportError:
33+
from typing_extensions import TYPE_CHECKING
34+
if TYPE_CHECKING:
3135
# Imported here due to circular import.
3236
from _pytest.main import Session # noqa: F401
3337

src/_pytest/outcomes.py

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

99
from packaging.version import Version
1010

11-
if False: # TYPE_CHECKING
11+
try:
12+
from typing import TYPE_CHECKING
13+
except ImportError:
14+
from typing_extensions import TYPE_CHECKING
15+
if TYPE_CHECKING:
1216
from typing import NoReturn
1317

1418

src/_pytest/pytester.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
from _pytest.pathlib import Path
3636
from _pytest.reports import TestReport
3737

38-
if False: # TYPE_CHECKING
38+
try:
39+
from typing import TYPE_CHECKING
40+
except ImportError:
41+
from typing_extensions import TYPE_CHECKING
42+
if TYPE_CHECKING:
3943
from typing import Type
4044

4145

@@ -189,7 +193,7 @@ def __repr__(self):
189193
del d["_name"]
190194
return "<ParsedCall {!r}(**{!r})>".format(self._name, d)
191195

192-
if False: # TYPE_CHECKING
196+
if TYPE_CHECKING:
193197
# The class has undetermined attributes, this tells mypy about it.
194198
def __getattr__(self, key):
195199
raise NotImplementedError()

src/_pytest/python_api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
from _pytest.compat import STRING_TYPES
2626
from _pytest.outcomes import fail
2727

28-
if False: # TYPE_CHECKING
28+
try:
29+
from typing import TYPE_CHECKING
30+
except ImportError:
31+
from typing_extensions import TYPE_CHECKING
32+
if TYPE_CHECKING:
2933
from typing import Type # noqa: F401 (used in type string)
3034

3135

src/_pytest/recwarn.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from _pytest.fixtures import yield_fixture
1616
from _pytest.outcomes import fail
1717

18-
if False: # TYPE_CHECKING
18+
try:
19+
from typing import TYPE_CHECKING
20+
except ImportError:
21+
from typing_extensions import TYPE_CHECKING
22+
if TYPE_CHECKING:
1923
from typing import Type
2024

2125

src/_pytest/runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
from _pytest.outcomes import Skipped
2121
from _pytest.outcomes import TEST_OUTCOME
2222

23-
if False: # TYPE_CHECKING
23+
try:
24+
from typing import TYPE_CHECKING
25+
except ImportError:
26+
from typing_extensions import TYPE_CHECKING
27+
if TYPE_CHECKING:
2428
from typing import Type
2529

2630
#

src/_pytest/warning_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import attr
66

77

8-
if False: # TYPE_CHECKING
8+
try:
9+
from typing import TYPE_CHECKING
10+
except ImportError:
11+
from typing_extensions import TYPE_CHECKING
12+
if TYPE_CHECKING:
913
from typing import Type # noqa: F401 (used in type string)
1014

1115

0 commit comments

Comments
 (0)