Repository navigation
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
Changes from all commits
2cfc68e
b97b72a
7354399
db38161
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2290,47 +2290,59 @@ cdef class Array(_PandasConvertible): | |||||
|
|
||||||
| def __add__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('add_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('add_checked', self, other) | ||||||
|
|
||||||
| def __truediv__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('divide_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('divide_checked', self, other) | ||||||
|
|
||||||
| def __mul__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('multiply_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('multiply_checked', self, other) | ||||||
|
|
||||||
| def __neg__(self): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('negate_checked', [self]) | ||||||
|
|
||||||
| def __pow__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('power_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('power_checked', self, other) | ||||||
|
|
||||||
| def __sub__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('subtract_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('subtract_checked', self, other) | ||||||
|
|
||||||
| def __and__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('bit_wise_and', [self, other]) | ||||||
| return _array_binop_or_notimplemented('bit_wise_and', self, other) | ||||||
|
|
||||||
| def __or__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('bit_wise_or', [self, other]) | ||||||
| return _array_binop_or_notimplemented('bit_wise_or', self, other) | ||||||
|
|
||||||
| def __xor__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('bit_wise_xor', [self, other]) | ||||||
| return _array_binop_or_notimplemented('bit_wise_xor', self, other) | ||||||
|
|
||||||
| def __lshift__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('shift_left_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('shift_left_checked', self, other) | ||||||
|
|
||||||
| def __rshift__(self, object other): | ||||||
| self._assert_cpu() | ||||||
| return _pc().call_function('shift_right_checked', [self, other]) | ||||||
| return _array_binop_or_notimplemented('shift_right_checked', self, other) | ||||||
|
|
||||||
|
|
||||||
| def _array_binop_or_notimplemented(op_name, left, right): | ||||||
| # Same NotImplemented fallback as Scalar.__add__ et al, see GH-49826. | ||||||
| # Only swallow TypeError for genuinely foreign types; propagate when | ||||||
| # the right operand is already Arrow-native so type errors are visible. | ||||||
| try: | ||||||
| return _pc().call_function(op_name, [left, right]) | ||||||
| except TypeError as e: | ||||||
| if isinstance(right, (Scalar, Array)): | ||||||
| raise | ||||||
|
Comment on lines
+2343
to
+2344
Member
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.
Suggested change
I actually think the original version was enough, see #49845 (comment). Sorry for that. |
||||||
| return NotImplemented | ||||||
|
|
||||||
|
|
||||||
| cdef _array_like_to_pandas(obj, options, types_mapper): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,37 +199,55 @@ 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 | ||
|
Member
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. Would remove these verbose comments here also. |
||
| # pyarrow.compute does not recognize so Python's reflected-operator | ||
| # fallback (__radd__ etc.) kicks in on user-defined classes. | ||
| # See GH-49826. | ||
| # | ||
| # Only swallow TypeError for genuinely foreign types (non-Arrow). If | ||
| # the right operand is already an Arrow Scalar or Array, any TypeError | ||
| # from call_function (e.g. mismatched Arrow types) should propagate so | ||
| # the caller sees a meaningful error rather than a silent NotImplemented. | ||
| try: | ||
| return _pc().call_function(op_name, [left, right]) | ||
| except TypeError as e: | ||
| if isinstance(right, (Scalar, Array)): | ||
| raise | ||
| return NotImplemented | ||
|
|
||
|
|
||
| _NULL = NA = None | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4401,6 +4401,16 @@ def test_non_cpu_array(): | |||||
| arr.validate(full=True) | ||||||
|
|
||||||
|
|
||||||
| def test_array_radd_unknown_operand(): | ||||||
| # GH-49826: Array.__add__ on an unknown right operand must return | ||||||
| # NotImplemented so Python falls back to right.__radd__. | ||||||
|
Comment on lines
+4405
to
+4406
Member
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.
Suggested change
Not sure these comments are needed. |
||||||
| class WithRadd: | ||||||
| def __radd__(self, other): | ||||||
| return "radd-called" | ||||||
|
|
||||||
| assert pa.array([1, 2]) + WithRadd() == "radd-called" | ||||||
|
|
||||||
|
|
||||||
| def test_arithmetic_dunders(): | ||||||
| # GH-32007 | ||||||
| arr1 = pa.array([-1.1, 2.2, -3.3]) | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1022,6 +1022,16 @@ def test_bitwise_dunders(): | |||||
| assert (scl2 >> scl1).equals(pc.shift_right_checked(scl2, scl1)) | ||||||
|
|
||||||
|
|
||||||
| def test_scalar_radd_unknown_operand(): | ||||||
| # GH-49826: Scalar.__add__ on an unknown right operand must return | ||||||
| # NotImplemented so Python falls back to right.__radd__. | ||||||
|
Comment on lines
+1026
to
+1027
Member
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.
Suggested change
Same here. |
||||||
| class WithRadd: | ||||||
| def __radd__(self, other): | ||||||
|
Member
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. Could we parametrize for other updated operands also? |
||||||
| return "radd-called" | ||||||
|
|
||||||
| assert pa.scalar(1) + WithRadd() == "radd-called" | ||||||
|
|
||||||
|
|
||||||
| def test_dunders_unmatching_types(): | ||||||
| # GH-32007 | ||||||
| error_match = r"Function '\w+' has no kernel matching input types" | ||||||
|
|
||||||
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.
I think comments are not necessary here.