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