Skip to content
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

B022: No arguments passed to contextlib.suppress #231

Merged
merged 6 commits into from Mar 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.rst
Expand Up @@ -141,6 +141,12 @@ garbage collection.
**B021**: f-string used as docstring. This will be interpreted by python
as a joined string rather than a docstring.

**B022**: No arguments passed to `contextlib.suppress`.
No exceptions will be suppressed and therefore this context manager is redundant.
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
N.B. this rule currently does not flag `suppress` calls to avoid potential false
positives due to similarly named user-defined functions.


Opinionated warnings
~~~~~~~~~~~~~~~~~~~~

Expand Down
22 changes: 22 additions & 0 deletions bugbear.py
Expand Up @@ -387,6 +387,7 @@ def visit_Raise(self, node):

def visit_With(self, node):
self.check_for_b017(node)
self.check_for_b022(node)
self.generic_visit(node)

def compose_call_path(self, node):
Expand Down Expand Up @@ -739,6 +740,20 @@ def check_for_b021(self, node):
B021(node.body[0].value.lineno, node.body[0].value.col_offset)
)

def check_for_b022(self, node):
item = node.items[0]
item_context = item.context_expr
if (
hasattr(item_context, "func")
and hasattr(item_context.func, "value")
and hasattr(item_context.func.value, "id")
and item_context.func.value.id == "contextlib"
and hasattr(item_context.func, "attr")
and item_context.func.attr == "suppress"
and len(item_context.args) == 0
):
self.errors.append(B022(node.lineno, node.col_offset))


@attr.s
class NameFinder(ast.NodeVisitor):
Expand Down Expand Up @@ -965,6 +980,13 @@ def visit(self, node):
"This will be interpreted by python as a joined string rather than a docstring."
)
)
B022 = Error(
message=(
"B022 No arguments passed to `contextlib.suppress`."
"No exceptions will be suppressed and therefore this"
"context manager is redundant."
)
)

# Warnings disabled by default.
B901 = Error(
Expand Down
19 changes: 19 additions & 0 deletions tests/b022.py
@@ -0,0 +1,19 @@
"""
Should emit:
B022 - on lines 8
"""

import contextlib

with contextlib.suppress():
raise ValueError

with contextlib.suppress(ValueError):
raise ValueError

exceptions_to_suppress = []
if True:
exceptions_to_suppress.append(ValueError)

with contextlib.suppress(*exceptions_to_suppress):
raise ValueError
7 changes: 7 additions & 0 deletions tests/test_bugbear.py
Expand Up @@ -32,6 +32,7 @@
B019,
B020,
B021,
B022,
B901,
B902,
B903,
Expand Down Expand Up @@ -303,6 +304,12 @@ def test_b021_classes(self):
)
self.assertEqual(errors, expected)

def test_b022(self):
filename = Path(__file__).absolute().parent / "b022.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
self.assertEqual(errors, self.errors(B022(8, 0)))

def test_b901(self):
filename = Path(__file__).absolute().parent / "b901.py"
bbc = BugBearChecker(filename=str(filename))
Expand Down