Skip to content

B005: Do not flag on imported modules #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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