Skip to content

Commit be50c14

Browse files
committed
Another solution.
1 parent 1cdafbd commit be50c14

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pyflakes/checker.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ def __init__(self, name, source):
325325
self.name = name
326326
self.source = source
327327
self.used = False
328-
self.during_type_checking = False
329328

330329
def __str__(self):
331330
return self.name
@@ -613,6 +612,17 @@ def _add_to_names(container):
613612

614613
class Scope(dict):
615614
importStarred = False # set to True when import * is found
615+
# Special key for checking whether a binding is defined only for type checking.
616+
TYPE_CHECKING_ONLY = object()
617+
618+
def __init__(self):
619+
super(Scope, self).__init__(self)
620+
self[self.TYPE_CHECKING_ONLY] = collections.defaultdict(bool)
621+
622+
def items(self):
623+
for key, val in super(Scope, self).items():
624+
if key != self.TYPE_CHECKING_ONLY:
625+
yield key, val
616626

617627
def __repr__(self):
618628
scope_cls = self.__class__.__name__
@@ -1119,8 +1129,6 @@ def addBinding(self, node, value):
11191129
break
11201130
existing = scope.get(value.name)
11211131

1122-
value.during_type_checking = self._in_type_checking
1123-
11241132
if (existing and not isinstance(existing, Builtin) and
11251133
not self.differentForks(node, existing.source)):
11261134

@@ -1152,6 +1160,8 @@ def addBinding(self, node, value):
11521160
# in scope
11531161
if value.name not in self.scope or not isinstance(value, Annotation):
11541162
self.scope[value.name] = value
1163+
self.scope[Scope.TYPE_CHECKING_ONLY][value.name] = self._in_type_checking
1164+
11551165

11561166
def _unknown_handler(self, node):
11571167
# this environment variable configures whether to error on unknown
@@ -1220,7 +1230,8 @@ def handleNodeLoad(self, node):
12201230
scope[n.fullName].used = (self.scope, node)
12211231
except KeyError:
12221232
pass
1223-
if n.during_type_checking and not self._in_annotation:
1233+
if (self.scope[Scope.TYPE_CHECKING_ONLY][name]
1234+
and not self._in_annotation):
12241235
# Only defined during type-checking; this does not count. Real code
12251236
# (not an annotation) using this binding will not work.
12261237
continue

0 commit comments

Comments
 (0)