Skip to content

Commit

Permalink
B005: Do not flag on imported modules (#353)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
FozzieHi and cooperlees authored Feb 16, 2023
1 parent 58dca8b commit 1bf567a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Change Log
Unreleased
~~~~~~~~~~

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

23.2.13
Expand Down
41 changes: 28 additions & 13 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class BugBearVisitor(ast.NodeVisitor):

NODE_WINDOW_SIZE = 4
_b023_seen = attr.ib(factory=set, init=False)
_b005_imports = attr.ib(factory=set, init=False)

if False:
# Useful for tracing what the hell is going on.
Expand Down Expand Up @@ -494,25 +495,39 @@ def visit_AnnAssign(self, node):
self.check_for_b032(node)
self.generic_visit(node)

def visit_Import(self, node):
self.check_for_b005(node)
self.generic_visit(node)

def check_for_b005(self, node):
if node.func.attr not in B005.methods:
return # method name doesn't match
if isinstance(node, ast.Import):
for name in node.names:
self._b005_imports.add(name.asname or name.name)
elif isinstance(node, ast.Call):
if node.func.attr not in B005.methods:
return # method name doesn't match

if (
isinstance(node.func.value, ast.Name)
and node.func.value.id in self._b005_imports
):
return # method is being run on an imported module

if len(node.args) != 1 or not isinstance(node.args[0], ast.Str):
return # used arguments don't match the builtin strip
if len(node.args) != 1 or not isinstance(node.args[0], ast.Str):
return # used arguments don't match the builtin strip

call_path = ".".join(compose_call_path(node.func.value))
if call_path in B005.valid_paths:
return # path is exempt
call_path = ".".join(compose_call_path(node.func.value))
if call_path in B005.valid_paths:
return # path is exempt

s = node.args[0].s
if len(s) == 1:
return # stripping just one character
s = node.args[0].s
if len(s) == 1:
return # stripping just one character

if len(s) == len(set(s)):
return # no characters appear more than once
if len(s) == len(set(s)):
return # no characters appear more than once

self.errors.append(B005(node.lineno, node.col_offset))
self.errors.append(B005(node.lineno, node.col_offset))

def check_for_b006_and_b008(self, node):
visitor = FuntionDefDefaultsVisitor(self.b008_extend_immutable_calls)
Expand Down
7 changes: 7 additions & 0 deletions tests/b005.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@
other_type().lstrip() # no warning
other_type().rstrip(["a", "b", "c"]) # no warning
other_type().strip("a", "b") # no warning

import test, test2 # isort: skip
import test_as as test3

test.strip("test") # no warning
test2.strip("test") # no warning
test3.strip("test") # no warning

0 comments on commit 1bf567a

Please sign in to comment.