Skip to content

B030: allow splats and calls #354

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 1 commit 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
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ MIT
Change Log
----------

Unreleased
~~~~~~~~~~

* B030: Allow calls and starred expressions in except handlers.

23.2.13
~~~~~~~~~

Expand Down
12 changes: 10 additions & 2 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,25 @@ def visit_ExceptHandler(self, node):
handlers = _flatten_excepthandler(node.type)
good_handlers = []
bad_handlers = []
ignored_handlers = []
for handler in handlers:
if isinstance(handler, (ast.Name, ast.Attribute)):
good_handlers.append(handler)
elif isinstance(handler, (ast.Call, ast.Starred)):
ignored_handlers.append(handler)
else:
bad_handlers.append(handler)
if bad_handlers:
self.errors.append(B030(node.lineno, node.col_offset))
names = [_to_name_str(e) for e in good_handlers]
if len(names) == 0 and not bad_handlers:
if len(names) == 0 and not bad_handlers and not ignored_handlers:
self.errors.append(B029(node.lineno, node.col_offset))
elif len(names) == 1 and not bad_handlers and isinstance(node.type, ast.Tuple):
elif (
len(names) == 1
and not bad_handlers
and not ignored_handlers
and isinstance(node.type, ast.Tuple)
):
self.errors.append(B013(node.lineno, node.col_offset, vars=names))
else:
maybe_error = _check_redundant_excepthandlers(names, node)
Expand Down
15 changes: 15 additions & 0 deletions tests/b030.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@
pass
except (1, ValueError): # error
pass

try:
pass
except (ValueError, *(RuntimeError, TypeError)): # ok
pass


def what_to_catch():
return (ValueError, TypeError)


try:
pass
except what_to_catch(): # ok
pass