Skip to content

Commit

Permalink
Fix bug in b030 (#364)
Browse files Browse the repository at this point in the history
* Update _flatten_excepthandler with more logic

* Shift some code left

* Reformat, _duh_

---------

Co-authored-by: Aaron Cunningham <aaron.cu@enervee.com>
  • Loading branch information
aacunningham and Aaron Cunningham authored Mar 9, 2023
1 parent dc34703 commit 37bd2a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 11 additions & 4 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ def _is_identifier(arg):


def _flatten_excepthandler(node):
if isinstance(node, ast.Tuple):
for elt in node.elts:
yield from _flatten_excepthandler(elt)
else:
if not isinstance(node, ast.Tuple):
yield node
return
expr_list = node.elts.copy()
while len(expr_list):
expr = expr_list.pop(0)
if isinstance(expr, ast.Starred) and isinstance(
expr.value, (ast.List, ast.Tuple)
):
expr_list.extend(expr.value.elts)
continue
yield expr


def _check_redundant_excepthandlers(names, node):
Expand Down
7 changes: 6 additions & 1 deletion tests/b030.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
try:
pass
except (ValueError, (RuntimeError, (KeyError, TypeError))): # ok
except (ValueError, (RuntimeError, (KeyError, TypeError))): # error
pass

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

try:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,9 @@ def test_b030(self):
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
expected = self.errors(
B030(8, 0),
B030(3, 0),
B030(13, 0),
B030(18, 0),
)
self.assertEqual(errors, expected)

Expand Down

0 comments on commit 37bd2a0

Please sign in to comment.