Skip to content

Commit a6f0dd2

Browse files
committed
Another solution.
1 parent 3b60143 commit a6f0dd2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

pyflakes/checker.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ def __init__(self, name, source):
314314
self.name = name
315315
self.source = source
316316
self.used = False
317-
self.during_type_checking = False
318317

319318
def __str__(self):
320319
return self.name
@@ -588,6 +587,17 @@ def _add_to_names(container):
588587

589588
class Scope(dict):
590589
importStarred = False # set to True when import * is found
590+
# Special key for checking whether a binding is defined only for type checking.
591+
TYPE_CHECKING_ONLY = object()
592+
593+
def __init__(self):
594+
super(Scope, self).__init__(self)
595+
self[self.TYPE_CHECKING_ONLY] = collections.defaultdict(bool)
596+
597+
def items(self):
598+
for key, val in super(Scope, self).items():
599+
if key != self.TYPE_CHECKING_ONLY:
600+
yield key, val
591601

592602
def __repr__(self):
593603
scope_cls = self.__class__.__name__
@@ -1074,8 +1084,6 @@ def addBinding(self, node, value):
10741084
break
10751085
existing = scope.get(value.name)
10761086

1077-
value.during_type_checking = self._in_type_checking
1078-
10791087
if (existing and not isinstance(existing, Builtin) and
10801088
not self.differentForks(node, existing.source)):
10811089

@@ -1103,6 +1111,7 @@ def addBinding(self, node, value):
11031111
# then assume the rebound name is used as a global or within a loop
11041112
value.used = self.scope[value.name].used
11051113

1114+
self.scope[Scope.TYPE_CHECKING_ONLY][value.name] = self._in_type_checking
11061115
self.scope[value.name] = value
11071116

11081117
def _unknown_handler(self, node):
@@ -1169,7 +1178,8 @@ def handleNodeLoad(self, node):
11691178
scope[n.fullName].used = (self.scope, node)
11701179
except KeyError:
11711180
pass
1172-
if n.during_type_checking and not self._in_annotation:
1181+
if (self.scope[Scope.TYPE_CHECKING_ONLY][name]
1182+
and not self._in_annotation):
11731183
# Only defined during type-checking; this does not count. Real code
11741184
# (not an annotation) using this binding will not work.
11751185
continue

0 commit comments

Comments
 (0)