Skip to content

Commit a795822

Browse files
committed
track if typing.TYPE_CHECKING to warn about non runtime bindings
When importing or defining values in ``if typing.TYPE_CHECKING`` blocks the bound names will not be available at runtime and may cause errors when used in the following way:: import typing if typing.TYPE_CHECKING: from module import Type # some slow import or circular reference def method(value) -> Type: # the import is needed by the type checker assert isinstance(value, Type) # this is a runtime error This change allows pyflakes to track what names are bound for runtime use, and allows it to warn when a non runtime name is used in a runtime context.
1 parent 40e6dc2 commit a795822

File tree

2 files changed

+155
-29
lines changed

2 files changed

+155
-29
lines changed

pyflakes/checker.py

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ class Binding(object):
321321
the node that this binding was last used.
322322
"""
323323

324-
def __init__(self, name, source):
324+
def __init__(self, name, source, runtime=True):
325325
self.name = name
326326
self.source = source
327327
self.used = False
328+
self.runtime = runtime
328329

329330
def __str__(self):
330331
return self.name
@@ -391,10 +392,10 @@ class Importation(Definition):
391392
@type fullName: C{str}
392393
"""
393394

394-
def __init__(self, name, source, full_name=None):
395+
def __init__(self, name, source, full_name=None, runtime=True):
395396
self.fullName = full_name or name
396397
self.redefined = []
397-
super(Importation, self).__init__(name, source)
398+
super(Importation, self).__init__(name, source, runtime=runtime)
398399

399400
def redefines(self, other):
400401
if isinstance(other, SubmoduleImportation):
@@ -439,11 +440,12 @@ class SubmoduleImportation(Importation):
439440
name is also the same, to avoid false positives.
440441
"""
441442

442-
def __init__(self, name, source):
443+
def __init__(self, name, source, runtime=True):
443444
# A dot should only appear in the name when it is a submodule import
444445
assert '.' in name and (not source or isinstance(source, ast.Import))
445446
package_name = name.split('.')[0]
446-
super(SubmoduleImportation, self).__init__(package_name, source)
447+
super(SubmoduleImportation, self).__init__(
448+
package_name, source, runtime=runtime)
447449
self.fullName = name
448450

449451
def redefines(self, other):
@@ -461,7 +463,8 @@ def source_statement(self):
461463

462464
class ImportationFrom(Importation):
463465

464-
def __init__(self, name, source, module, real_name=None):
466+
def __init__(
467+
self, name, source, module, real_name=None, runtime=True):
465468
self.module = module
466469
self.real_name = real_name or name
467470

@@ -470,7 +473,8 @@ def __init__(self, name, source, module, real_name=None):
470473
else:
471474
full_name = module + '.' + self.real_name
472475

473-
super(ImportationFrom, self).__init__(name, source, full_name)
476+
super(ImportationFrom, self).__init__(
477+
name, source, full_name, runtime=runtime)
474478

475479
def __str__(self):
476480
"""Return import full name with alias."""
@@ -492,8 +496,8 @@ def source_statement(self):
492496
class StarImportation(Importation):
493497
"""A binding created by a 'from x import *' statement."""
494498

495-
def __init__(self, name, source):
496-
super(StarImportation, self).__init__('*', source)
499+
def __init__(self, name, source, runtime=True):
500+
super(StarImportation, self).__init__('*', source, runtime=runtime)
497501
# Each star importation needs a unique name, and
498502
# may not be the module name otherwise it will be deemed imported
499503
self.name = name + '.*'
@@ -572,7 +576,7 @@ class ExportBinding(Binding):
572576
C{__all__} will not have an unused import warning reported for them.
573577
"""
574578

575-
def __init__(self, name, source, scope):
579+
def __init__(self, name, source, scope, runtime=True):
576580
if '__all__' in scope and isinstance(source, ast.AugAssign):
577581
self.names = list(scope['__all__'].names)
578582
else:
@@ -603,7 +607,7 @@ def _add_to_names(container):
603607
# If not list concatenation
604608
else:
605609
break
606-
super(ExportBinding, self).__init__(name, source)
610+
super(ExportBinding, self).__init__(name, source, runtime=runtime)
607611

608612

609613
class Scope(dict):
@@ -867,6 +871,7 @@ class Checker(object):
867871
traceTree = False
868872
_in_annotation = AnnotationState.NONE
869873
_in_deferred = False
874+
_in_type_check_guard = False
870875

871876
builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
872877
_customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
@@ -1140,9 +1145,11 @@ def addBinding(self, node, value):
11401145
# then assume the rebound name is used as a global or within a loop
11411146
value.used = self.scope[value.name].used
11421147

1143-
# don't treat annotations as assignments if there is an existing value
1144-
# in scope
1145-
if value.name not in self.scope or not isinstance(value, Annotation):
1148+
# always allow the first assignment or if not already a runtime value,
1149+
# but do not shadow an existing assignment with an annotation or non
1150+
# runtime value.
1151+
if (not existing or not existing.runtime or (
1152+
not isinstance(value, Annotation) and value.runtime)):
11461153
self.scope[value.name] = value
11471154

11481155
def _unknown_handler(self, node):
@@ -1201,12 +1208,18 @@ def handleNodeLoad(self, node):
12011208
self.report(messages.InvalidPrintSyntax, node)
12021209

12031210
try:
1204-
scope[name].used = (self.scope, node)
1211+
n = scope[name]
1212+
if (not n.runtime and not (
1213+
self._in_type_check_guard
1214+
or self._in_annotation)):
1215+
self.report(messages.UndefinedName, node, name)
1216+
return
1217+
1218+
n.used = (self.scope, node)
12051219

12061220
# if the name of SubImportation is same as
12071221
# alias of other Importation and the alias
12081222
# is used, SubImportation also should be marked as used.
1209-
n = scope[name]
12101223
if isinstance(n, Importation) and n._has_alias():
12111224
try:
12121225
scope[n.fullName].used = (self.scope, node)
@@ -1269,18 +1282,20 @@ def handleNodeStore(self, node):
12691282
break
12701283

12711284
parent_stmt = self.getParent(node)
1285+
runtime = not self._in_type_check_guard
12721286
if isinstance(parent_stmt, ANNASSIGN_TYPES) and parent_stmt.value is None:
1273-
binding = Annotation(name, node)
1287+
binding = Annotation(name, node, runtime=runtime)
12741288
elif isinstance(parent_stmt, (FOR_TYPES, ast.comprehension)) or (
12751289
parent_stmt != node._pyflakes_parent and
12761290
not self.isLiteralTupleUnpacking(parent_stmt)):
1277-
binding = Binding(name, node)
1291+
binding = Binding(name, node, runtime=runtime)
12781292
elif name == '__all__' and isinstance(self.scope, ModuleScope):
1279-
binding = ExportBinding(name, node._pyflakes_parent, self.scope)
1293+
binding = ExportBinding(
1294+
name, node._pyflakes_parent, self.scope, runtime=runtime)
12801295
elif PY2 and isinstance(getattr(node, 'ctx', None), ast.Param):
1281-
binding = Argument(name, self.getScopeNode(node))
1296+
binding = Argument(name, self.getScopeNode(node), runtime=runtime)
12821297
else:
1283-
binding = Assignment(name, node)
1298+
binding = Assignment(name, node, runtime=runtime)
12841299
self.addBinding(node, binding)
12851300

12861301
def handleNodeDelete(self, node):
@@ -1969,7 +1984,40 @@ def DICT(self, node):
19691984
def IF(self, node):
19701985
if isinstance(node.test, ast.Tuple) and node.test.elts != []:
19711986
self.report(messages.IfTuple, node)
1972-
self.handleChildren(node)
1987+
1988+
self._handle_type_comments(node)
1989+
self.handleNode(node.test, node)
1990+
1991+
# check if the body/orelse should be handled specially because it is
1992+
# a if TYPE_CHECKING guard.
1993+
test = node.test
1994+
reverse = False
1995+
if isinstance(test, ast.UnaryOp) and isinstance(test.op, ast.Not):
1996+
test = test.operand
1997+
reverse = True
1998+
1999+
type_checking = _is_typing(test, 'TYPE_CHECKING', self.scopeStack)
2000+
orig = self._in_type_check_guard
2001+
2002+
# normalize body and orelse to a list
2003+
body, orelse = (
2004+
i if isinstance(i, list) else [i]
2005+
for i in (node.body, node.orelse))
2006+
2007+
# set the guard and handle the body
2008+
if type_checking and not reverse:
2009+
self._in_type_check_guard = True
2010+
2011+
for n in body:
2012+
self.handleNode(n, node)
2013+
2014+
# set the guard and handle the orelse
2015+
if type_checking:
2016+
self._in_type_check_guard = True if reverse else orig
2017+
2018+
for n in orelse:
2019+
self.handleNode(n, node)
2020+
self._in_type_check_guard = orig
19732021

19742022
IFEXP = IF
19752023

@@ -2092,7 +2140,10 @@ def FUNCTIONDEF(self, node):
20922140
for deco in node.decorator_list:
20932141
self.handleNode(deco, node)
20942142
self.LAMBDA(node)
2095-
self.addBinding(node, FunctionDefinition(node.name, node))
2143+
self.addBinding(
2144+
node,
2145+
FunctionDefinition(
2146+
node.name, node, runtime=not self._in_type_check_guard))
20962147
# doctest does not process doctest within a doctest,
20972148
# or in nested functions.
20982149
if (self.withDoctest and
@@ -2217,7 +2268,10 @@ def CLASSDEF(self, node):
22172268
for stmt in node.body:
22182269
self.handleNode(stmt, node)
22192270
self.popScope()
2220-
self.addBinding(node, ClassDefinition(node.name, node))
2271+
self.addBinding(
2272+
node,
2273+
ClassDefinition(
2274+
node.name, node, runtime=not self._in_type_check_guard))
22212275

22222276
def AUGASSIGN(self, node):
22232277
self.handleNodeLoad(node.target)
@@ -2250,12 +2304,15 @@ def TUPLE(self, node):
22502304
LIST = TUPLE
22512305

22522306
def IMPORT(self, node):
2307+
runtime = not self._in_type_check_guard
22532308
for alias in node.names:
22542309
if '.' in alias.name and not alias.asname:
2255-
importation = SubmoduleImportation(alias.name, node)
2310+
importation = SubmoduleImportation(
2311+
alias.name, node, runtime=runtime)
22562312
else:
22572313
name = alias.asname or alias.name
2258-
importation = Importation(name, node, alias.name)
2314+
importation = Importation(
2315+
name, node, alias.name, runtime=runtime)
22592316
self.addBinding(node, importation)
22602317

22612318
def IMPORTFROM(self, node):
@@ -2268,6 +2325,7 @@ def IMPORTFROM(self, node):
22682325

22692326
module = ('.' * node.level) + (node.module or '')
22702327

2328+
runtime = not self._in_type_check_guard
22712329
for alias in node.names:
22722330
name = alias.asname or alias.name
22732331
if node.module == '__future__':
@@ -2286,10 +2344,10 @@ def IMPORTFROM(self, node):
22862344

22872345
self.scope.importStarred = True
22882346
self.report(messages.ImportStarUsed, node, module)
2289-
importation = StarImportation(module, node)
2347+
importation = StarImportation(module, node, runtime=runtime)
22902348
else:
2291-
importation = ImportationFrom(name, node,
2292-
module, alias.name)
2349+
importation = ImportationFrom(
2350+
name, node, module, alias.name, runtime=runtime)
22932351
self.addBinding(node, importation)
22942352

22952353
def TRY(self, node):

pyflakes/test/test_type_annotations.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,57 @@ def f(): # type: () -> T
682682
pass
683683
""")
684684

685+
@skipIf(version_info < (3,), 'new in Python 3')
686+
def test_typing_guard_import(self):
687+
# T is imported for runtime use
688+
self.flakes("""
689+
from typing import TYPE_CHECKING
690+
691+
if TYPE_CHECKING:
692+
from t import T
693+
694+
def f(x) -> T:
695+
from t import T
696+
697+
assert isinstance(x, T)
698+
return x
699+
""")
700+
# T is defined at runtime in one side of the if/else block
701+
self.flakes("""
702+
from typing import TYPE_CHECKING, Union
703+
704+
if TYPE_CHECKING:
705+
from t import T
706+
else:
707+
T = object
708+
709+
if not TYPE_CHECKING:
710+
U = object
711+
else:
712+
from t import U
713+
714+
def f(x) -> Union[T, U]:
715+
assert isinstance(x, (T, U))
716+
return x
717+
""")
718+
719+
@skipIf(version_info < (3,), 'new in Python 3')
720+
def test_typing_guard_import_runtime_error(self):
721+
# T and U are not bound for runtime use
722+
self.flakes("""
723+
from typing import TYPE_CHECKING, Union
724+
725+
if TYPE_CHECKING:
726+
from t import T
727+
728+
class U:
729+
pass
730+
731+
def f(x) -> Union[T, U]:
732+
assert isinstance(x, (T, U))
733+
return x
734+
""", m.UndefinedName, m.UndefinedName)
735+
685736
def test_typing_guard_for_protocol(self):
686737
self.flakes("""
687738
from typing import TYPE_CHECKING
@@ -696,6 +747,23 @@ def f(): # type: () -> int
696747
pass
697748
""")
698749

750+
def test_typing_guard_with_elif_branch(self):
751+
# This test will not raise an error even though Protocol is not
752+
# defined outside TYPE_CHECKING because Pyflakes does not do case
753+
# analysis.
754+
self.flakes("""
755+
from typing import TYPE_CHECKING
756+
if TYPE_CHECKING:
757+
from typing import Protocol
758+
elif False:
759+
Protocol = object
760+
else:
761+
pass
762+
class C(Protocol):
763+
def f(): # type: () -> int
764+
pass
765+
""")
766+
699767
def test_typednames_correct_forward_ref(self):
700768
self.flakes("""
701769
from typing import TypedDict, List, NamedTuple

0 commit comments

Comments
 (0)