@@ -314,7 +314,6 @@ def __init__(self, name, source):
314
314
self .name = name
315
315
self .source = source
316
316
self .used = False
317
- self .during_type_checking = False
318
317
319
318
def __str__ (self ):
320
319
return self .name
@@ -588,6 +587,17 @@ def _add_to_names(container):
588
587
589
588
class Scope (dict ):
590
589
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
591
601
592
602
def __repr__ (self ):
593
603
scope_cls = self .__class__ .__name__
@@ -1074,8 +1084,6 @@ def addBinding(self, node, value):
1074
1084
break
1075
1085
existing = scope .get (value .name )
1076
1086
1077
- value .during_type_checking = self ._in_type_checking
1078
-
1079
1087
if (existing and not isinstance (existing , Builtin ) and
1080
1088
not self .differentForks (node , existing .source )):
1081
1089
@@ -1103,6 +1111,7 @@ def addBinding(self, node, value):
1103
1111
# then assume the rebound name is used as a global or within a loop
1104
1112
value .used = self .scope [value .name ].used
1105
1113
1114
+ self .scope [Scope .TYPE_CHECKING_ONLY ][value .name ] = self ._in_type_checking
1106
1115
self .scope [value .name ] = value
1107
1116
1108
1117
def _unknown_handler (self , node ):
@@ -1169,7 +1178,8 @@ def handleNodeLoad(self, node):
1169
1178
scope [n .fullName ].used = (self .scope , node )
1170
1179
except KeyError :
1171
1180
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 ):
1173
1183
# Only defined during type-checking; this does not count. Real code
1174
1184
# (not an annotation) using this binding will not work.
1175
1185
continue
0 commit comments