-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-49826: [Python] Scalar arithmetic dunders return NotImplemented on unknown operand types #49833
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
Open
SAY-5
wants to merge
1
commit into
apache:main
Choose a base branch
from
SAY-5:fix/pyarrow-scalar-dunders-notimplemented-49826
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+21
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,37 +199,48 @@ cdef class Scalar(_Weakrefable): | |
| return _pc().call_function('abs_checked', [self]) | ||
|
|
||
| def __add__(self, object other): | ||
| return _pc().call_function('add_checked', [self, other]) | ||
| return _binop_or_notimplemented('add_checked', self, other) | ||
|
|
||
| def __truediv__(self, object other): | ||
| return _pc().call_function('divide_checked', [self, other]) | ||
| return _binop_or_notimplemented('divide_checked', self, other) | ||
|
|
||
| def __mul__(self, object other): | ||
| return _pc().call_function('multiply_checked', [self, other]) | ||
| return _binop_or_notimplemented('multiply_checked', self, other) | ||
|
|
||
| def __neg__(self): | ||
| return _pc().call_function('negate_checked', [self]) | ||
|
|
||
| def __pow__(self, object other): | ||
| return _pc().call_function('power_checked', [self, other]) | ||
| return _binop_or_notimplemented('power_checked', self, other) | ||
|
|
||
| def __sub__(self, object other): | ||
| return _pc().call_function('subtract_checked', [self, other]) | ||
| return _binop_or_notimplemented('subtract_checked', self, other) | ||
|
|
||
| def __and__(self, object other): | ||
| return _pc().call_function('bit_wise_and', [self, other]) | ||
| return _binop_or_notimplemented('bit_wise_and', self, other) | ||
|
|
||
| def __or__(self, object other): | ||
| return _pc().call_function('bit_wise_or', [self, other]) | ||
| return _binop_or_notimplemented('bit_wise_or', self, other) | ||
|
|
||
| def __xor__(self, object other): | ||
| return _pc().call_function('bit_wise_xor', [self, other]) | ||
| return _binop_or_notimplemented('bit_wise_xor', self, other) | ||
|
|
||
| def __lshift__(self, object other): | ||
| return _pc().call_function('shift_left_checked', [self, other]) | ||
| return _binop_or_notimplemented('shift_left_checked', self, other) | ||
|
|
||
| def __rshift__(self, object other): | ||
| return _pc().call_function('shift_right_checked', [self, other]) | ||
| return _binop_or_notimplemented('shift_right_checked', self, other) | ||
|
|
||
|
|
||
| def _binop_or_notimplemented(op_name, left, right): | ||
| # Scalar arithmetic dunders must return NotImplemented for argument types | ||
| # pyarrow.compute does not recognize so Python's reflected-operator | ||
| # fallback (__radd__ etc.) kicks in on user-defined classes. | ||
| # See GH-49826. | ||
| try: | ||
| return _pc().call_function(op_name, [left, right]) | ||
| except TypeError: | ||
| return NotImplemented | ||
|
Comment on lines
+242
to
+243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with @AlenkaF here, the exception should be properly raised and chained: except TypeError as e:
raise NotImplemented from e |
||
|
|
||
|
|
||
| _NULL = NA = None | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change this in a way that errors coming from
call_functionwould not end up becomingNotImplemented?