Skip to content

Commit 58dca8b

Browse files
B030: allow splats and calls (#354)
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.
1 parent 7323815 commit 58dca8b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ MIT
325325
Change Log
326326
----------
327327

328+
Unreleased
329+
~~~~~~~~~~
330+
331+
* B030: Allow calls and starred expressions in except handlers.
332+
328333
23.2.13
329334
~~~~~~~~~
330335

bugbear.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,25 @@ def visit_ExceptHandler(self, node):
333333
handlers = _flatten_excepthandler(node.type)
334334
good_handlers = []
335335
bad_handlers = []
336+
ignored_handlers = []
336337
for handler in handlers:
337338
if isinstance(handler, (ast.Name, ast.Attribute)):
338339
good_handlers.append(handler)
340+
elif isinstance(handler, (ast.Call, ast.Starred)):
341+
ignored_handlers.append(handler)
339342
else:
340343
bad_handlers.append(handler)
341344
if bad_handlers:
342345
self.errors.append(B030(node.lineno, node.col_offset))
343346
names = [_to_name_str(e) for e in good_handlers]
344-
if len(names) == 0 and not bad_handlers:
347+
if len(names) == 0 and not bad_handlers and not ignored_handlers:
345348
self.errors.append(B029(node.lineno, node.col_offset))
346-
elif len(names) == 1 and not bad_handlers and isinstance(node.type, ast.Tuple):
349+
elif (
350+
len(names) == 1
351+
and not bad_handlers
352+
and not ignored_handlers
353+
and isinstance(node.type, ast.Tuple)
354+
):
347355
self.errors.append(B013(node.lineno, node.col_offset, vars=names))
348356
else:
349357
maybe_error = _check_redundant_excepthandlers(names, node)

tests/b030.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@
1212
pass
1313
except (1, ValueError): # error
1414
pass
15+
16+
try:
17+
pass
18+
except (ValueError, *(RuntimeError, TypeError)): # ok
19+
pass
20+
21+
22+
def what_to_catch():
23+
return (ValueError, TypeError)
24+
25+
26+
try:
27+
pass
28+
except what_to_catch(): # ok
29+
pass

0 commit comments

Comments
 (0)