Skip to content

Commit fea5483

Browse files
[wrong-exception-operation] Fix FP for tuple concatenation of exception types (#9289) (#9291)
(cherry picked from commit 8d4c6c1) Co-authored-by: Jacob Walls <[email protected]>
1 parent d0d5c91 commit fea5483

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix false positive for ``invalid-exception-operation`` when concatenating tuples
2+
of exception types.
3+
4+
Closes #9288

pylint/checkers/exceptions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,19 @@ def gather_exceptions_from_handler(
533533
@utils.only_required_for_messages("wrong-exception-operation")
534534
def visit_binop(self, node: nodes.BinOp) -> None:
535535
if isinstance(node.parent, nodes.ExceptHandler):
536+
both_sides_tuple_or_uninferable = isinstance(
537+
utils.safe_infer(node.left), (nodes.Tuple, util.UninferableBase)
538+
) and isinstance(
539+
utils.safe_infer(node.right), (nodes.Tuple, util.UninferableBase)
540+
)
541+
# Tuple concatenation allowed
542+
if both_sides_tuple_or_uninferable:
543+
if node.op == "+":
544+
return
545+
suggestion = f"Did you mean '({node.left.as_string()} + {node.right.as_string()})' instead?"
536546
# except (V | A)
537-
suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
547+
else:
548+
suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
538549
self.add_message("wrong-exception-operation", node=node, args=(suggestion,))
539550

540551
@utils.only_required_for_messages("wrong-exception-operation")

tests/functional/w/wrong_exception_operation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,24 @@
1616
1/0
1717
except (ValueError < TypeError): # [wrong-exception-operation]
1818
pass
19+
20+
21+
# Concatenation of exception type tuples
22+
DIVISION_BY_ZERO = (ZeroDivisionError,)
23+
VALUE_ERROR = (ValueError,)
24+
UNINFERABLE = DIVISION_BY_ZERO | VALUE_ERROR
25+
26+
try:
27+
1/0
28+
except (ValueError, ) + DIVISION_BY_ZERO:
29+
pass
30+
31+
try:
32+
1/0
33+
except (ValueError, ) | DIVISION_BY_ZERO: # [wrong-exception-operation]
34+
pass
35+
36+
try:
37+
1/0
38+
except (ValueError, ) + UNINFERABLE:
39+
pass

tests/functional/w/wrong_exception_operation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ catching-non-exception:6:8:6:30::"Catching an exception which doesn't inherit fr
22
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
33
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
44
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
5+
wrong-exception-operation:33:7:33:40::Invalid exception operation. Did you mean '((ValueError, ) + DIVISION_BY_ZERO)' instead?:UNDEFINED

0 commit comments

Comments
 (0)