@@ -321,11 +321,12 @@ class Binding(object):
321
321
the node that this binding was last used.
322
322
"""
323
323
324
- def __init__ (self , name , source ):
324
+ def __init__ (self , name , source , during_type_checking ):
325
325
self .name = name
326
326
self .source = source
327
327
self .used = False
328
- self .during_type_checking = False
328
+ assert isinstance (during_type_checking , bool )
329
+ self .during_type_checking = during_type_checking
329
330
330
331
def __str__ (self ):
331
332
return self .name
@@ -350,7 +351,7 @@ class Builtin(Definition):
350
351
"""A definition created for all Python builtins."""
351
352
352
353
def __init__ (self , name ):
353
- super (Builtin , self ).__init__ (name , None )
354
+ super (Builtin , self ).__init__ (name , None , during_type_checking = False )
354
355
355
356
def __repr__ (self ):
356
357
return '<%s object %r at 0x%x>' % (self .__class__ .__name__ ,
@@ -392,10 +393,11 @@ class Importation(Definition):
392
393
@type fullName: C{str}
393
394
"""
394
395
395
- def __init__ (self , name , source , full_name = None ):
396
+ def __init__ (self , name , source , during_type_checking , full_name = None ):
396
397
self .fullName = full_name or name
397
398
self .redefined = []
398
- super (Importation , self ).__init__ (name , source )
399
+ super (Importation , self ).__init__ (name , source ,
400
+ during_type_checking = during_type_checking )
399
401
400
402
def redefines (self , other ):
401
403
if isinstance (other , SubmoduleImportation ):
@@ -440,11 +442,12 @@ class SubmoduleImportation(Importation):
440
442
name is also the same, to avoid false positives.
441
443
"""
442
444
443
- def __init__ (self , name , source ):
445
+ def __init__ (self , name , source , during_type_checking ):
444
446
# A dot should only appear in the name when it is a submodule import
445
447
assert '.' in name and (not source or isinstance (source , ast .Import ))
446
448
package_name = name .split ('.' )[0 ]
447
- super (SubmoduleImportation , self ).__init__ (package_name , source )
449
+ super (SubmoduleImportation , self ).__init__ (
450
+ package_name , source , during_type_checking = during_type_checking )
448
451
self .fullName = name
449
452
450
453
def redefines (self , other ):
@@ -462,7 +465,7 @@ def source_statement(self):
462
465
463
466
class ImportationFrom (Importation ):
464
467
465
- def __init__ (self , name , source , module , real_name = None ):
468
+ def __init__ (self , name , source , module , during_type_checking , real_name = None ):
466
469
self .module = module
467
470
self .real_name = real_name or name
468
471
@@ -471,7 +474,8 @@ def __init__(self, name, source, module, real_name=None):
471
474
else :
472
475
full_name = module + '.' + self .real_name
473
476
474
- super (ImportationFrom , self ).__init__ (name , source , full_name )
477
+ super (ImportationFrom , self ).__init__ (name , source , full_name = full_name ,
478
+ during_type_checking = during_type_checking )
475
479
476
480
def __str__ (self ):
477
481
"""Return import full name with alias."""
@@ -493,8 +497,9 @@ def source_statement(self):
493
497
class StarImportation (Importation ):
494
498
"""A binding created by a 'from x import *' statement."""
495
499
496
- def __init__ (self , name , source ):
497
- super (StarImportation , self ).__init__ ('*' , source )
500
+ def __init__ (self , name , source , during_type_checking ):
501
+ super (StarImportation , self ).__init__ ('*' , source ,
502
+ during_type_checking = during_type_checking )
498
503
# Each star importation needs a unique name, and
499
504
# may not be the module name otherwise it will be deemed imported
500
505
self .name = name + '.*'
@@ -520,14 +525,17 @@ class FutureImportation(ImportationFrom):
520
525
"""
521
526
522
527
def __init__ (self , name , source , scope ):
523
- super (FutureImportation , self ).__init__ (name , source , '__future__' )
528
+ super (FutureImportation , self ).__init__ (name , source , '__future__' ,
529
+ during_type_checking = False )
524
530
self .used = (scope , source )
525
531
526
532
527
533
class Argument (Binding ):
528
534
"""
529
535
Represents binding a name as an argument.
530
536
"""
537
+ def __init__ (self , name , source ):
538
+ super (Argument , self ).__init__ (name , source , during_type_checking = False )
531
539
532
540
533
541
class Assignment (Binding ):
@@ -577,7 +585,7 @@ class ExportBinding(Binding):
577
585
C{__all__} will not have an unused import warning reported for them.
578
586
"""
579
587
580
- def __init__ (self , name , source , scope ):
588
+ def __init__ (self , name , source , scope , during_type_checking ):
581
589
if '__all__' in scope and isinstance (source , ast .AugAssign ):
582
590
self .names = list (scope ['__all__' ].names )
583
591
else :
@@ -608,7 +616,7 @@ def _add_to_names(container):
608
616
# If not list concatenation
609
617
else :
610
618
break
611
- super (ExportBinding , self ).__init__ (name , source )
619
+ super (ExportBinding , self ).__init__ (name , source , during_type_checking )
612
620
613
621
614
622
class Scope (dict ):
@@ -1119,8 +1127,6 @@ def addBinding(self, node, value):
1119
1127
break
1120
1128
existing = scope .get (value .name )
1121
1129
1122
- value .during_type_checking = self ._in_type_checking
1123
-
1124
1130
if (existing and not isinstance (existing , Builtin ) and
1125
1131
not self .differentForks (node , existing .source )):
1126
1132
@@ -1288,13 +1294,15 @@ def handleNodeStore(self, node):
1288
1294
elif isinstance (parent_stmt , (FOR_TYPES , ast .comprehension )) or (
1289
1295
parent_stmt != node ._pyflakes_parent and
1290
1296
not self .isLiteralTupleUnpacking (parent_stmt )):
1291
- binding = Binding (name , node )
1297
+ binding = Binding (name , node , during_type_checking = self . _in_type_checking )
1292
1298
elif name == '__all__' and isinstance (self .scope , ModuleScope ):
1293
- binding = ExportBinding (name , node ._pyflakes_parent , self .scope )
1299
+ binding = ExportBinding (name , node ._pyflakes_parent , self .scope ,
1300
+ during_type_checking = self ._in_type_checking )
1294
1301
elif PY2 and isinstance (getattr (node , 'ctx' , None ), ast .Param ):
1295
- binding = Argument (name , self .getScopeNode (node ))
1302
+ binding = Argument (name , self .getScopeNode (node ),
1303
+ during_type_checking = self ._in_type_checking )
1296
1304
else :
1297
- binding = Assignment (name , node )
1305
+ binding = Assignment (name , node , during_type_checking = self . _in_type_checking )
1298
1306
self .addBinding (node , binding )
1299
1307
1300
1308
def handleNodeDelete (self , node ):
@@ -2009,7 +2017,8 @@ def GLOBAL(self, node):
2009
2017
2010
2018
# One 'global' statement can bind multiple (comma-delimited) names.
2011
2019
for node_name in node .names :
2012
- node_value = Assignment (node_name , node )
2020
+ node_value = Assignment (node_name , node ,
2021
+ during_type_checking = self ._in_type_checking )
2013
2022
2014
2023
# Remove UndefinedName messages already reported for this name.
2015
2024
# TODO: if the global is not used in this scope, it does not
@@ -2111,7 +2120,8 @@ def FUNCTIONDEF(self, node):
2111
2120
for deco in node .decorator_list :
2112
2121
self .handleNode (deco , node )
2113
2122
self .LAMBDA (node )
2114
- self .addBinding (node , FunctionDefinition (node .name , node ))
2123
+ self .addBinding (node , FunctionDefinition (
2124
+ node .name , node , during_type_checking = self ._in_type_checking ))
2115
2125
# doctest does not process doctest within a doctest,
2116
2126
# or in nested functions.
2117
2127
if (self .withDoctest and
@@ -2236,7 +2246,8 @@ def CLASSDEF(self, node):
2236
2246
for stmt in node .body :
2237
2247
self .handleNode (stmt , node )
2238
2248
self .popScope ()
2239
- self .addBinding (node , ClassDefinition (node .name , node ))
2249
+ self .addBinding (node , ClassDefinition (
2250
+ node .name , node , during_type_checking = self ._in_type_checking ))
2240
2251
2241
2252
def AUGASSIGN (self , node ):
2242
2253
self .handleNodeLoad (node .target )
@@ -2271,10 +2282,12 @@ def TUPLE(self, node):
2271
2282
def IMPORT (self , node ):
2272
2283
for alias in node .names :
2273
2284
if '.' in alias .name and not alias .asname :
2274
- importation = SubmoduleImportation (alias .name , node )
2285
+ importation = SubmoduleImportation (
2286
+ alias .name , node , during_type_checking = self ._in_type_checking )
2275
2287
else :
2276
2288
name = alias .asname or alias .name
2277
- importation = Importation (name , node , alias .name )
2289
+ importation = Importation (name , node , full_name = alias .name ,
2290
+ during_type_checking = self ._in_type_checking )
2278
2291
self .addBinding (node , importation )
2279
2292
2280
2293
def IMPORTFROM (self , node ):
@@ -2299,16 +2312,17 @@ def IMPORTFROM(self, node):
2299
2312
elif alias .name == '*' :
2300
2313
# Only Python 2, local import * is a SyntaxWarning
2301
2314
if not PY2 and not isinstance (self .scope , ModuleScope ):
2302
- self .report (messages .ImportStarNotPermitted ,
2303
- node , module )
2315
+ self .report (messages .ImportStarNotPermitted , node , module )
2304
2316
continue
2305
2317
2306
2318
self .scope .importStarred = True
2307
2319
self .report (messages .ImportStarUsed , node , module )
2308
- importation = StarImportation (module , node )
2320
+ importation = StarImportation (
2321
+ module , node , during_type_checking = self ._in_type_checking )
2309
2322
else :
2310
- importation = ImportationFrom (name , node ,
2311
- module , alias .name )
2323
+ importation = ImportationFrom (
2324
+ name , node , module , real_name = alias .name ,
2325
+ during_type_checking = self ._in_type_checking )
2312
2326
self .addBinding (node , importation )
2313
2327
2314
2328
def TRY (self , node ):
0 commit comments