@@ -293,6 +293,7 @@ class BugBearVisitor(ast.NodeVisitor):
293
293
294
294
NODE_WINDOW_SIZE = 4
295
295
_b023_seen = attr .ib (factory = set , init = False )
296
+ _b005_imports = attr .ib (factory = set , init = False )
296
297
297
298
if False :
298
299
# Useful for tracing what the hell is going on.
@@ -494,25 +495,39 @@ def visit_AnnAssign(self, node):
494
495
self .check_for_b032 (node )
495
496
self .generic_visit (node )
496
497
498
+ def visit_Import (self , node ):
499
+ self .check_for_b005 (node )
500
+ self .generic_visit (node )
501
+
497
502
def check_for_b005 (self , node ):
498
- if node .func .attr not in B005 .methods :
499
- return # method name doesn't match
503
+ if isinstance (node , ast .Import ):
504
+ for name in node .names :
505
+ self ._b005_imports .add (name .asname or name .name )
506
+ elif isinstance (node , ast .Call ):
507
+ if node .func .attr not in B005 .methods :
508
+ return # method name doesn't match
509
+
510
+ if (
511
+ isinstance (node .func .value , ast .Name )
512
+ and node .func .value .id in self ._b005_imports
513
+ ):
514
+ return # method is being run on an imported module
500
515
501
- if len (node .args ) != 1 or not isinstance (node .args [0 ], ast .Str ):
502
- return # used arguments don't match the builtin strip
516
+ if len (node .args ) != 1 or not isinstance (node .args [0 ], ast .Str ):
517
+ return # used arguments don't match the builtin strip
503
518
504
- call_path = "." .join (compose_call_path (node .func .value ))
505
- if call_path in B005 .valid_paths :
506
- return # path is exempt
519
+ call_path = "." .join (compose_call_path (node .func .value ))
520
+ if call_path in B005 .valid_paths :
521
+ return # path is exempt
507
522
508
- s = node .args [0 ].s
509
- if len (s ) == 1 :
510
- return # stripping just one character
523
+ s = node .args [0 ].s
524
+ if len (s ) == 1 :
525
+ return # stripping just one character
511
526
512
- if len (s ) == len (set (s )):
513
- return # no characters appear more than once
527
+ if len (s ) == len (set (s )):
528
+ return # no characters appear more than once
514
529
515
- self .errors .append (B005 (node .lineno , node .col_offset ))
530
+ self .errors .append (B005 (node .lineno , node .col_offset ))
516
531
517
532
def check_for_b006_and_b008 (self , node ):
518
533
visitor = FuntionDefDefaultsVisitor (self .b008_extend_immutable_calls )
0 commit comments