@@ -310,11 +310,12 @@ class Binding(object):
310
310
the node that this binding was last used.
311
311
"""
312
312
313
- def __init__ (self , name , source ):
313
+ def __init__ (self , name , source , during_type_checking ):
314
314
self .name = name
315
315
self .source = source
316
316
self .used = False
317
- self .during_type_checking = False
317
+ assert isinstance (during_type_checking , bool )
318
+ self .during_type_checking = during_type_checking
318
319
319
320
def __str__ (self ):
320
321
return self .name
@@ -339,7 +340,7 @@ class Builtin(Definition):
339
340
"""A definition created for all Python builtins."""
340
341
341
342
def __init__ (self , name ):
342
- super (Builtin , self ).__init__ (name , None )
343
+ super (Builtin , self ).__init__ (name , None , during_type_checking = False )
343
344
344
345
def __repr__ (self ):
345
346
return '<%s object %r at 0x%x>' % (self .__class__ .__name__ ,
@@ -381,10 +382,11 @@ class Importation(Definition):
381
382
@type fullName: C{str}
382
383
"""
383
384
384
- def __init__ (self , name , source , full_name = None ):
385
+ def __init__ (self , name , source , during_type_checking , full_name = None ):
385
386
self .fullName = full_name or name
386
387
self .redefined = []
387
- super (Importation , self ).__init__ (name , source )
388
+ super (Importation , self ).__init__ (name , source ,
389
+ during_type_checking = during_type_checking )
388
390
389
391
def redefines (self , other ):
390
392
if isinstance (other , SubmoduleImportation ):
@@ -429,11 +431,12 @@ class SubmoduleImportation(Importation):
429
431
name is also the same, to avoid false positives.
430
432
"""
431
433
432
- def __init__ (self , name , source ):
434
+ def __init__ (self , name , source , during_type_checking ):
433
435
# A dot should only appear in the name when it is a submodule import
434
436
assert '.' in name and (not source or isinstance (source , ast .Import ))
435
437
package_name = name .split ('.' )[0 ]
436
- super (SubmoduleImportation , self ).__init__ (package_name , source )
438
+ super (SubmoduleImportation , self ).__init__ (
439
+ package_name , source , during_type_checking = during_type_checking )
437
440
self .fullName = name
438
441
439
442
def redefines (self , other ):
@@ -451,7 +454,7 @@ def source_statement(self):
451
454
452
455
class ImportationFrom (Importation ):
453
456
454
- def __init__ (self , name , source , module , real_name = None ):
457
+ def __init__ (self , name , source , module , during_type_checking , real_name = None ):
455
458
self .module = module
456
459
self .real_name = real_name or name
457
460
@@ -460,7 +463,8 @@ def __init__(self, name, source, module, real_name=None):
460
463
else :
461
464
full_name = module + '.' + self .real_name
462
465
463
- super (ImportationFrom , self ).__init__ (name , source , full_name )
466
+ super (ImportationFrom , self ).__init__ (name , source , full_name = full_name ,
467
+ during_type_checking = during_type_checking )
464
468
465
469
def __str__ (self ):
466
470
"""Return import full name with alias."""
@@ -482,8 +486,9 @@ def source_statement(self):
482
486
class StarImportation (Importation ):
483
487
"""A binding created by a 'from x import *' statement."""
484
488
485
- def __init__ (self , name , source ):
486
- super (StarImportation , self ).__init__ ('*' , source )
489
+ def __init__ (self , name , source , during_type_checking ):
490
+ super (StarImportation , self ).__init__ ('*' , source ,
491
+ during_type_checking = during_type_checking )
487
492
# Each star importation needs a unique name, and
488
493
# may not be the module name otherwise it will be deemed imported
489
494
self .name = name + '.*'
@@ -509,14 +514,17 @@ class FutureImportation(ImportationFrom):
509
514
"""
510
515
511
516
def __init__ (self , name , source , scope ):
512
- super (FutureImportation , self ).__init__ (name , source , '__future__' )
517
+ super (FutureImportation , self ).__init__ (name , source , '__future__' ,
518
+ during_type_checking = False )
513
519
self .used = (scope , source )
514
520
515
521
516
522
class Argument (Binding ):
517
523
"""
518
524
Represents binding a name as an argument.
519
525
"""
526
+ def __init__ (self , name , source ):
527
+ super (Argument , self ).__init__ (name , source , during_type_checking = False )
520
528
521
529
522
530
class Assignment (Binding ):
@@ -552,7 +560,7 @@ class ExportBinding(Binding):
552
560
C{__all__} will not have an unused import warning reported for them.
553
561
"""
554
562
555
- def __init__ (self , name , source , scope ):
563
+ def __init__ (self , name , source , scope , during_type_checking ):
556
564
if '__all__' in scope and isinstance (source , ast .AugAssign ):
557
565
self .names = list (scope ['__all__' ].names )
558
566
else :
@@ -583,7 +591,7 @@ def _add_to_names(container):
583
591
# If not list concatenation
584
592
else :
585
593
break
586
- super (ExportBinding , self ).__init__ (name , source )
594
+ super (ExportBinding , self ).__init__ (name , source , during_type_checking )
587
595
588
596
589
597
class Scope (dict ):
@@ -1074,8 +1082,6 @@ def addBinding(self, node, value):
1074
1082
break
1075
1083
existing = scope .get (value .name )
1076
1084
1077
- value .during_type_checking = self ._in_type_checking
1078
-
1079
1085
if (existing and not isinstance (existing , Builtin ) and
1080
1086
not self .differentForks (node , existing .source )):
1081
1087
@@ -1233,13 +1239,15 @@ def handleNodeStore(self, node):
1233
1239
if isinstance (parent_stmt , (FOR_TYPES , ast .comprehension )) or (
1234
1240
parent_stmt != node ._pyflakes_parent and
1235
1241
not self .isLiteralTupleUnpacking (parent_stmt )):
1236
- binding = Binding (name , node )
1242
+ binding = Binding (name , node , during_type_checking = self . _in_type_checking )
1237
1243
elif name == '__all__' and isinstance (self .scope , ModuleScope ):
1238
- binding = ExportBinding (name , node ._pyflakes_parent , self .scope )
1244
+ binding = ExportBinding (name , node ._pyflakes_parent , self .scope ,
1245
+ during_type_checking = self ._in_type_checking )
1239
1246
elif PY2 and isinstance (getattr (node , 'ctx' , None ), ast .Param ):
1240
- binding = Argument (name , self .getScopeNode (node ))
1247
+ binding = Argument (name , self .getScopeNode (node ),
1248
+ during_type_checking = self ._in_type_checking )
1241
1249
else :
1242
- binding = Assignment (name , node )
1250
+ binding = Assignment (name , node , during_type_checking = self . _in_type_checking )
1243
1251
self .addBinding (node , binding )
1244
1252
1245
1253
def handleNodeDelete (self , node ):
@@ -1867,7 +1875,8 @@ def GLOBAL(self, node):
1867
1875
1868
1876
# One 'global' statement can bind multiple (comma-delimited) names.
1869
1877
for node_name in node .names :
1870
- node_value = Assignment (node_name , node )
1878
+ node_value = Assignment (node_name , node ,
1879
+ during_type_checking = self ._in_type_checking )
1871
1880
1872
1881
# Remove UndefinedName messages already reported for this name.
1873
1882
# TODO: if the global is not used in this scope, it does not
@@ -1969,7 +1978,8 @@ def FUNCTIONDEF(self, node):
1969
1978
for deco in node .decorator_list :
1970
1979
self .handleNode (deco , node )
1971
1980
self .LAMBDA (node )
1972
- self .addBinding (node , FunctionDefinition (node .name , node ))
1981
+ self .addBinding (node , FunctionDefinition (
1982
+ node .name , node , during_type_checking = self ._in_type_checking ))
1973
1983
# doctest does not process doctest within a doctest,
1974
1984
# or in nested functions.
1975
1985
if (self .withDoctest and
@@ -2094,7 +2104,8 @@ def CLASSDEF(self, node):
2094
2104
for stmt in node .body :
2095
2105
self .handleNode (stmt , node )
2096
2106
self .popScope ()
2097
- self .addBinding (node , ClassDefinition (node .name , node ))
2107
+ self .addBinding (node , ClassDefinition (
2108
+ node .name , node , during_type_checking = self ._in_type_checking ))
2098
2109
2099
2110
def AUGASSIGN (self , node ):
2100
2111
self .handleNodeLoad (node .target )
@@ -2129,10 +2140,12 @@ def TUPLE(self, node):
2129
2140
def IMPORT (self , node ):
2130
2141
for alias in node .names :
2131
2142
if '.' in alias .name and not alias .asname :
2132
- importation = SubmoduleImportation (alias .name , node )
2143
+ importation = SubmoduleImportation (
2144
+ alias .name , node , during_type_checking = self ._in_type_checking )
2133
2145
else :
2134
2146
name = alias .asname or alias .name
2135
- importation = Importation (name , node , alias .name )
2147
+ importation = Importation (name , node , full_name = alias .name ,
2148
+ during_type_checking = self ._in_type_checking )
2136
2149
self .addBinding (node , importation )
2137
2150
2138
2151
def IMPORTFROM (self , node ):
@@ -2157,16 +2170,17 @@ def IMPORTFROM(self, node):
2157
2170
elif alias .name == '*' :
2158
2171
# Only Python 2, local import * is a SyntaxWarning
2159
2172
if not PY2 and not isinstance (self .scope , ModuleScope ):
2160
- self .report (messages .ImportStarNotPermitted ,
2161
- node , module )
2173
+ self .report (messages .ImportStarNotPermitted , node , module )
2162
2174
continue
2163
2175
2164
2176
self .scope .importStarred = True
2165
2177
self .report (messages .ImportStarUsed , node , module )
2166
- importation = StarImportation (module , node )
2178
+ importation = StarImportation (
2179
+ module , node , during_type_checking = self ._in_type_checking )
2167
2180
else :
2168
- importation = ImportationFrom (name , node ,
2169
- module , alias .name )
2181
+ importation = ImportationFrom (
2182
+ name , node , module , real_name = alias .name ,
2183
+ during_type_checking = self ._in_type_checking )
2170
2184
self .addBinding (node , importation )
2171
2185
2172
2186
def TRY (self , node ):
0 commit comments