Skip to content

Commit 1bf567a

Browse files
FozzieHicooperlees
andauthored
B005: Do not flag on imported modules (#353)
* B005: Do not flag on imported modules * Fix formatting in tests * Update README.rst I screwed up the README - So fixed. --------- Co-authored-by: Cooper Lees <me@cooperlees.com>
1 parent 58dca8b commit 1bf567a

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ Change Log
328328
Unreleased
329329
~~~~~~~~~~
330330

331+
* B005: Do not flag when using the ``strip()`` method on an imported module.
331332
* B030: Allow calls and starred expressions in except handlers.
332333

333334
23.2.13

bugbear.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ class BugBearVisitor(ast.NodeVisitor):
293293

294294
NODE_WINDOW_SIZE = 4
295295
_b023_seen = attr.ib(factory=set, init=False)
296+
_b005_imports = attr.ib(factory=set, init=False)
296297

297298
if False:
298299
# Useful for tracing what the hell is going on.
@@ -494,25 +495,39 @@ def visit_AnnAssign(self, node):
494495
self.check_for_b032(node)
495496
self.generic_visit(node)
496497

498+
def visit_Import(self, node):
499+
self.check_for_b005(node)
500+
self.generic_visit(node)
501+
497502
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
500515

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
503518

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
507522

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
511526

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
514529

515-
self.errors.append(B005(node.lineno, node.col_offset))
530+
self.errors.append(B005(node.lineno, node.col_offset))
516531

517532
def check_for_b006_and_b008(self, node):
518533
visitor = FuntionDefDefaultsVisitor(self.b008_extend_immutable_calls)

tests/b005.py

+7
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@
2424
other_type().lstrip() # no warning
2525
other_type().rstrip(["a", "b", "c"]) # no warning
2626
other_type().strip("a", "b") # no warning
27+
28+
import test, test2 # isort: skip
29+
import test_as as test3
30+
31+
test.strip("test") # no warning
32+
test2.strip("test") # no warning
33+
test3.strip("test") # no warning

0 commit comments

Comments
 (0)