Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/3.0.x] [wrong-exception-operation] Fix FP for tuple concatenation of exception types #9291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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