Skip to content

Commit d66aeb8

Browse files
committed
Make the Binding parameter a constructor and use it for all bindings.
1 parent 44aab70 commit d66aeb8

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
@@ -310,11 +310,11 @@ class Binding(object):
310310
the node that this binding was last used.
311311
"""
312312

313-
def __init__(self, name, source):
313+
def __init__(self, name, source, during_type_checking=False):
314314
self.name = name
315315
self.source = source
316316
self.used = False
317-
self.during_type_checking = False
317+
self.during_type_checking = during_type_checking
318318

319319
def __str__(self):
320320
return self.name
@@ -1074,6 +1074,8 @@ def addBinding(self, node, value):
10741074
break
10751075
existing = scope.get(value.name)
10761076

1077+
value.during_type_checking = self._in_type_checking
1078+
10771079
if (existing and not isinstance(existing, Builtin) and
10781080
not self.differentForks(node, existing.source)):
10791081

@@ -1162,15 +1164,15 @@ def handleNodeLoad(self, node):
11621164
# alias of other Importation and the alias
11631165
# is used, SubImportation also should be marked as used.
11641166
n = scope[name]
1165-
if isinstance(n, Importation):
1166-
if n._has_alias():
1167-
try:
1168-
scope[n.fullName].used = (self.scope, node)
1169-
except KeyError:
1170-
pass
1171-
if n.during_type_checking and not self._in_annotation:
1172-
# Only defined during type-checking; this does not count.
1173-
continue
1167+
if isinstance(n, Importation) and n._has_alias():
1168+
try:
1169+
scope[n.fullName].used = (self.scope, node)
1170+
except KeyError:
1171+
pass
1172+
if n.during_type_checking and not self._in_annotation:
1173+
# Only defined during type-checking; this does not count. Real code
1174+
# (not an annotation) using this binding will not work.
1175+
continue
11741176
except KeyError:
11751177
pass
11761178
else:
@@ -2131,7 +2133,6 @@ def IMPORT(self, node):
21312133
else:
21322134
name = alias.asname or alias.name
21332135
importation = Importation(name, node, alias.name)
2134-
importation.during_type_checking = self._in_type_checking
21352136
self.addBinding(node, importation)
21362137

21372138
def IMPORTFROM(self, node):
@@ -2166,7 +2167,6 @@ def IMPORTFROM(self, node):
21662167
else:
21672168
importation = ImportationFrom(name, node,
21682169
module, alias.name)
2169-
importation.during_type_checking = self._in_type_checking
21702170
self.addBinding(node, importation)
21712171

21722172
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)