Skip to content

Commit 37bd2a0

Browse files
aacunninghamAaron Cunningham
andauthored
Fix bug in b030 (#364)
* Update _flatten_excepthandler with more logic * Shift some code left * Reformat, _duh_ --------- Co-authored-by: Aaron Cunningham <[email protected]>
1 parent dc34703 commit 37bd2a0

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

bugbear.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,18 @@ def _is_identifier(arg):
190190

191191

192192
def _flatten_excepthandler(node):
193-
if isinstance(node, ast.Tuple):
194-
for elt in node.elts:
195-
yield from _flatten_excepthandler(elt)
196-
else:
193+
if not isinstance(node, ast.Tuple):
197194
yield node
195+
return
196+
expr_list = node.elts.copy()
197+
while len(expr_list):
198+
expr = expr_list.pop(0)
199+
if isinstance(expr, ast.Starred) and isinstance(
200+
expr.value, (ast.List, ast.Tuple)
201+
):
202+
expr_list.extend(expr.value.elts)
203+
continue
204+
yield expr
198205

199206

200207
def _check_redundant_excepthandlers(names, node):

tests/b030.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
try:
22
pass
3-
except (ValueError, (RuntimeError, (KeyError, TypeError))): # ok
3+
except (ValueError, (RuntimeError, (KeyError, TypeError))): # error
4+
pass
5+
6+
try:
7+
pass
8+
except (ValueError, *(RuntimeError, *(KeyError, TypeError))): # ok
49
pass
510

611
try:

tests/test_bugbear.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ def test_b030(self):
456456
bbc = BugBearChecker(filename=str(filename))
457457
errors = list(bbc.run())
458458
expected = self.errors(
459-
B030(8, 0),
459+
B030(3, 0),
460460
B030(13, 0),
461+
B030(18, 0),
461462
)
462463
self.assertEqual(errors, expected)
463464

0 commit comments

Comments
 (0)