@@ -325,7 +325,6 @@ def __init__(self, name, source):
325
325
self.name = name
326
326
self.source = source
327
327
self.used = False
328
- self.during_type_checking = False
329
328
330
329
def __str__(self):
331
330
return self.name
@@ -613,6 +612,17 @@ def _add_to_names(container):
613
612
614
613
class Scope(dict):
615
614
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
616
626
617
627
def __repr__(self):
618
628
scope_cls = self.__class__.__name__
@@ -1119,8 +1129,6 @@ def addBinding(self, node, value):
1119
1129
break
1120
1130
existing = scope.get(value.name)
1121
1131
1122
- value.during_type_checking = self._in_type_checking
1123
-
1124
1132
if (existing and not isinstance(existing, Builtin) and
1125
1133
not self.differentForks(node, existing.source)):
1126
1134
@@ -1152,6 +1160,8 @@ def addBinding(self, node, value):
1152
1160
# in scope
1153
1161
if value.name not in self.scope or not isinstance(value, Annotation):
1154
1162
self.scope[value.name] = value
1163
+ self.scope[Scope.TYPE_CHECKING_ONLY][value.name] = self._in_type_checking
1164
+
1155
1165
1156
1166
def _unknown_handler(self, node):
1157
1167
# this environment variable configures whether to error on unknown
@@ -1220,7 +1230,8 @@ def handleNodeLoad(self, node):
1220
1230
scope[n.fullName].used = (self.scope, node)
1221
1231
except KeyError:
1222
1232
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):
1224
1235
# Only defined during type-checking; this does not count. Real code
1225
1236
# (not an annotation) using this binding will not work.
1226
1237
continue
0 commit comments