Skip to content

Commit

Permalink
B030: allow splats and calls (#354)
Browse files Browse the repository at this point in the history
Fixes #352.

I allow calls in addition to splats because a real-world false positive was reported
and buggy code doesn't seem common enough to merit an on-by-default error.
  • Loading branch information
JelleZijlstra authored Feb 16, 2023
1 parent 7323815 commit 58dca8b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit 58dca8b

Please sign in to comment.