8000 Support typing.TYPE_CHECKING. · python/mypy@345a457 · GitHub
[go: up one dir, main page]

Skip to content

Commit 345a457

Browse files
author
Guido van Rossum
committed
Support typing.TYPE_CHECKING.
Fixes #2165. This is not completely correct (it'll trigger on *any* variable named TYPE_CHECKING) but I think that's acceptable. We should be able to phase out MYPY once this is in the next release.
1 parent 60a99a7 commit 345a457

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

mypy/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,7 @@ def dispatch(sources: List[BuildSource], manager: BuildManager) -> None:
14721472
manager.log("Loaded graph with %d nodes" % len(graph))
14731473
process_graph(graph, manager)
14741474
if manager.options.warn_unused_ignores:
1475+
# TODO: This could also be a per-file option.
14751476
manager.errors.generate_unused_ignore_notes()
14761477

14771478

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ def infer_if_condition_value(expr: Node, pyversion: Tuple[int, int], platform: s
30233023
result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE
30243024
elif name == 'PY3':
30253025
result = ALWAYS_TRUE if pyversion[0] == 3 else ALWAYS_FALSE
3026-
elif name == 'MYPY':
3026+
elif name == 'MYPY' or name == 'TYPE_CHECKING':
30273027
result = ALWAYS_TRUE
30283028
if negated:
30293029
if result == ALWAYS_TRUE:

test-data/unit/check-unreachable-code.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,48 @@ else:
9090
None + ''
9191
[builtins fixtures/bool.pyi]
9292

93+
[case testTypeCheckingConditional]
94+
import typing
95+
if typing.TYPE_CHECKING:
96+
import pow123 # E
97+
else:
98+
import xyz753
99+
[out]
100+
main:3: error: Cannot find module named 'pow123'
101+
main:3: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)
102+
103+
[case testTypeCheckingConditionalFromImport]
104+
from typing import TYPE_CHECKING
105+
if TYPE_CHECKING:
106+
import pow123 # E
107+
else:
108+
import xyz753
109+
[out]
110+
main:3: error: Cannot find module named 'pow123'
111+
main:3: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)
112+
113+
[case testNegatedTypeCheckingConditional]
114+
import typing
115+
if not typing.TYPE_CHECKING:
116+
import pow123 # E
117+
else:
118+
import xyz753
119+
[builtins fixtures/bool.pyi]
120+
[out]
121+
main:5: error: Cannot find module named 'xyz753'
122+
main:5: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)
123+
124+
[case testUndefinedTypeCheckingConditional]
125+
if not TYPE_CHECKING: # E
126+
import pow123
127+
else:
128+
import xyz753
129+
[builtins fixtures/bool.pyi]
130+
[out]
131+
main:1: error: Name 'TYPE_CHECKING' is not defined
132+
main:4: error: Cannot find module named 'xyz753'
133+
main:4: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)
134+
93135
[case testConditionalClassDefPY3]
94136
def f(): pass
95137
PY3 = f()

test-data/unit/lib-stub/typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ def NewType(name: str, tp: Type[T]) -> Callable[[T], T]:
8484
def new_type(x):
8585
return x
8686
return new_type
87+
88+
TYPE_CHECKING = 1

0 commit comments

Comments
 (0)
0