Skip to content

Commit a043ccd

Browse files
committed
Make the Binding parameter a constructor and use it for all bindings.
1 parent 827f048 commit a043ccd

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

pyflakes/checker.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +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, during_type_checking=False):
325325
self.name = name
326326
self.source = source
327327
self.used = False
328-
self.during_type_checking = False
328+
self.during_type_checking = during_type_checking
329329

330330
def __str__(self):
331331
return self.name
@@ -1119,6 +1119,8 @@ def addBinding(self, node, value):
11191119
break
11201120
existing = scope.get(value.name)
11211121

1122+
value.during_type_checking = self._in_type_checking
1123+
11221124
if (existing and not isinstance(existing, Builtin) and
11231125
not self.differentForks(node, existing.source)):
11241126

@@ -1213,15 +1215,15 @@ def handleNodeLoad(self, node):
12131215
# alias of other Importation and the alias
12141216
# is used, SubImportation also should be marked as used.
12151217
n = scope[name]
1216-
if isinstance(n, Importation):
1217-
if n._has_alias():
1218-
try:
1219-
scope[n.fullName].used = (self.scope, node)
1220-
except KeyError:
1221-
pass
1222-
if n.during_type_checking and not self._in_annotation:
1223-
# Only defined during type-checking; this does not count.
1224-
continue
1218+
if isinstance(n, Importation) and n._has_alias():
1219+
try:
1220+
scope[n.fullName].used = (self.scope, node)
1221+
except KeyError:
1222+
pass
1223+
if n.during_type_checking and not self._in_annotation:
1224+
# Only defined during type-checking; this does not count. Real code
1225+
# (not an annotation) using this binding will not work.
1226+
continue
12251227
except KeyError:
12261228
pass
12271229
else:
@@ -2271,7 +2273,6 @@ def IMPORT(self, node):
22712273
else:
22722274
name = alias.asname or alias.name
22732275
importation = Importation(name, node, alias.name)
2274-
importation.during_type_checking = self._in_type_checking
22752276
self.addBinding(node, importation)
22762277

22772278
def IMPORTFROM(self, node):
@@ -2306,7 +2307,6 @@ def IMPORTFROM(self, node):
23062307
else:
23072308
importation = ImportationFrom(name, node,
23082309
module, alias.name)
2309-
importation.during_type_checking = self._in_type_checking
23102310
self.addBinding(node, importation)
23112311

23122312
def TRY(self, node):

pyflakes/test/test_imports.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,16 @@ def test_ignoresTypingImports(self):
10401040
b()
10411041
''', m.UndefinedName)
10421042

1043+
def test_ignoresTypingClassDefinitino(self):
1044+
"""Ignores definitions within 'if TYPE_CHECKING' checking normal code."""
1045+
self.flakes('''
1046+
from typing import TYPE_CHECKING
1047+
if TYPE_CHECKING:
1048+
class T:
1049+
...
1050+
t = T()
1051+
''', m.UndefinedName)
1052+
10431053
def test_usesTypingImportsForAnnotations(self):
10441054
"""Uses imports within 'if TYPE_CHECKING' checking annotations."""
10451055
self.flakes('''

0 commit comments

Comments
 (0)