Skip to content

Commit

Permalink
Support typing.TYPE_CHECKING.
Browse files Browse the repository at this point in the history
Fixes python#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.
  • Loading branch information
Guido van Rossum committed Sep 21, 2016
1 parent 60a99a7 commit 345a457
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ def dispatch(sources: List[BuildSource], manager: BuildManager) -> None:
manager.log("Loaded graph with %d nodes" % len(graph))
process_graph(graph, manager)
if manager.options.warn_unused_ignores:
# TODO: This could also be a per-file option.
manager.errors.generate_unused_ignore_notes()


Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3023,7 +3023,7 @@ def infer_if_condition_value(expr: Node, pyversion: Tuple[int, int], platform: s
result = ALWAYS_TRUE if pyversion[0] == 2 else ALWAYS_FALSE
elif name == 'PY3':
result = ALWAYS_TRUE if pyversion[0] == 3 else ALWAYS_FALSE
elif name == 'MYPY':
elif name == 'MYPY' or name == 'TYPE_CHECKING':
result = ALWAYS_TRUE
if negated:
if result == ALWAYS_TRUE:
Expand Down
42 changes: 42 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,48 @@ else:
None + ''
[builtins fixtures/bool.pyi]

[case testTypeCheckingConditional]
import typing
if typing.TYPE_CHECKING:
import pow123 # E
else:
import xyz753
[out]
main:3: error: Cannot find module named 'pow123'
main:3: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)

[case testTypeCheckingConditionalFromImport]
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import pow123 # E
else:
import xyz753
[out]
main:3: error: Cannot find module named 'pow123'
main:3: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)

[case testNegatedTypeCheckingConditional]
import typing
if not typing.TYPE_CHECKING:
import pow123 # E
else:
import xyz753
[builtins fixtures/bool.pyi]
[out]
main:5: error: Cannot find module named 'xyz753'
main:5: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)

[case testUndefinedTypeCheckingConditional]
if not TYPE_CHECKING: # E
import pow123
else:
import xyz753
[builtins fixtures/bool.pyi]
[out]
main:1: error: Name 'TYPE_CHECKING' is not defined
main:4: error: Cannot find module named 'xyz753'
main:4: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)

[case testConditionalClassDefPY3]
def f(): pass
PY3 = f()
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ def NewType(name: str, tp: Type[T]) -> Callable[[T], T]:
def new_type(x):
return x
return new_type

TYPE_CHECKING = 1

0 comments on commit 345a457

Please sign in to comment.