Skip to content

Commit

Permalink
[wrong-exception-operation] Fix FP for tuple concatenation of excepti…
Browse files Browse the repository at this point in the history
…on types (#9289)
  • Loading branch information
jacobtylerwalls authored Dec 10, 2023
1 parent 796eae3 commit 8d4c6c1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9288.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix false positive for ``invalid-exception-operation`` when concatenating tuples
of exception types.

Closes #9288
13 changes: 12 additions & 1 deletion pylint/checkers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,19 @@ def gather_exceptions_from_handler(
@utils.only_required_for_messages("wrong-exception-operation")
def visit_binop(self, node: nodes.BinOp) -> None:
if isinstance(node.parent, nodes.ExceptHandler):
both_sides_tuple_or_uninferable = isinstance(
utils.safe_infer(node.left), (nodes.Tuple, util.UninferableBase)
) and isinstance(
utils.safe_infer(node.right), (nodes.Tuple, util.UninferableBase)
)
# Tuple concatenation allowed
if both_sides_tuple_or_uninferable:
if node.op == "+":
return
suggestion = f"Did you mean '({node.left.as_string()} + {node.right.as_string()})' instead?"
# except (V | A)
suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
else:
suggestion = f"Did you mean '({node.left.as_string()}, {node.right.as_string()})' instead?"
self.add_message("wrong-exception-operation", node=node, args=(suggestion,))

@utils.only_required_for_messages("wrong-exception-operation")
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/w/wrong_exception_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@
1/0
except (ValueError < TypeError): # [wrong-exception-operation]
pass


# Concatenation of exception type tuples
DIVISION_BY_ZERO = (ZeroDivisionError,)
VALUE_ERROR = (ValueError,)
UNINFERABLE = DIVISION_BY_ZERO | VALUE_ERROR

try:
1/0
except (ValueError, ) + DIVISION_BY_ZERO:
pass

try:
1/0
except (ValueError, ) | DIVISION_BY_ZERO: # [wrong-exception-operation]
pass

try:
1/0
except (ValueError, ) + UNINFERABLE:
pass
1 change: 1 addition & 0 deletions tests/functional/w/wrong_exception_operation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ catching-non-exception:6:8:6:30::"Catching an exception which doesn't inherit fr
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:33:7:33:40::Invalid exception operation. Did you mean '((ValueError, ) + DIVISION_BY_ZERO)' instead?:UNDEFINED

0 comments on commit 8d4c6c1

Please sign in to comment.