diff --git a/python/pyarrow-stubs-tests/test_scalar.py b/python/pyarrow-stubs-tests/test_scalar.py index e27a2229680..bb00f12386a 100644 --- a/python/pyarrow-stubs-tests/test_scalar.py +++ b/python/pyarrow-stubs-tests/test_scalar.py @@ -82,10 +82,11 @@ def test_scalar_operators() -> None: check(assert_type(s << 1, pa.Scalar), pa.Int64Scalar) check(assert_type(s >> 1, pa.Scalar), pa.Int64Scalar) + check(assert_type(1 + s, pa.Scalar), pa.Int64Scalar) + check(assert_type(1 - pa.array([2]), pa.Array), pa.Int64Array) + if TYPE_CHECKING: - # Scalars don't implement the reflected operators - 1 + s # type: ignore[operator] # ty: ignore[unsupported-operator] - # nor floor division and modulo + # Scalars don't implement floor division and modulo s // 2 # type: ignore[operator] # ty: ignore[unsupported-operator] s % 2 # type: ignore[operator] # ty: ignore[unsupported-operator] diff --git a/python/pyarrow-stubs/__lib/array.pyi b/python/pyarrow-stubs/__lib/array.pyi index 87244f36419..29148997f94 100644 --- a/python/pyarrow-stubs/__lib/array.pyi +++ b/python/pyarrow-stubs/__lib/array.pyi @@ -313,6 +313,16 @@ class Array(_PandasConvertible[pd.Series]): __xor__ = __add__ __lshift__ = __add__ __rshift__ = __add__ + __radd__ = __add__ + __rsub__ = __add__ + __rmul__ = __add__ + __rtruediv__ = __add__ + __rpow__ = __add__ + __rand__ = __add__ + __ror__ = __add__ + __rxor__ = __add__ + __rlshift__ = __add__ + __rrshift__ = __add__ @overload def __pow__(self, other: ChunkedArray, mod: None = None, /) -> ChunkedArray: ... @overload diff --git a/python/pyarrow-stubs/__lib/scalar.pyi b/python/pyarrow-stubs/__lib/scalar.pyi index f206434db26..b9b77d11246 100644 --- a/python/pyarrow-stubs/__lib/scalar.pyi +++ b/python/pyarrow-stubs/__lib/scalar.pyi @@ -75,6 +75,16 @@ class Scalar(_Weakrefable): __xor__ = __add__ __lshift__ = __add__ __rshift__ = __add__ + __radd__ = __add__ + __rsub__ = __add__ + __rmul__ = __add__ + __rtruediv__ = __add__ + __rpow__ = __add__ + __rand__ = __add__ + __ror__ = __add__ + __rxor__ = __add__ + __rlshift__ = __add__ + __rrshift__ = __add__ @overload def __pow__(self, other: ChunkedArray, mod: None = None, /) -> ChunkedArray: ... @overload diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index ac3b8733463..4c076c4dca8 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -2444,14 +2444,26 @@ cdef class Array(_PandasConvertible): self._assert_cpu() return _compute_binary_op('add_checked', self, other) + def __radd__(self, object other): + self._assert_cpu() + return _compute_binary_op('add_checked', other, self) + def __truediv__(self, object other): self._assert_cpu() return _compute_binary_op('divide_checked', self, other) + def __rtruediv__(self, object other): + self._assert_cpu() + return _compute_binary_op('divide_checked', other, self) + def __mul__(self, object other): self._assert_cpu() return _compute_binary_op('multiply_checked', self, other) + def __rmul__(self, object other): + self._assert_cpu() + return _compute_binary_op('multiply_checked', other, self) + def __neg__(self): self._assert_cpu() return _pc().call_function('negate_checked', [self]) @@ -2460,30 +2472,58 @@ cdef class Array(_PandasConvertible): self._assert_cpu() return _compute_binary_op('power_checked', self, other) + def __rpow__(self, object other): + self._assert_cpu() + return _compute_binary_op('power_checked', other, self) + def __sub__(self, object other): self._assert_cpu() return _compute_binary_op('subtract_checked', self, other) + def __rsub__(self, object other): + self._assert_cpu() + return _compute_binary_op('subtract_checked', other, self) + def __and__(self, object other): self._assert_cpu() return _compute_binary_op('bit_wise_and', self, other) + def __rand__(self, object other): + self._assert_cpu() + return _compute_binary_op('bit_wise_and', other, self) + def __or__(self, object other): self._assert_cpu() return _compute_binary_op('bit_wise_or', self, other) + def __ror__(self, object other): + self._assert_cpu() + return _compute_binary_op('bit_wise_or', other, self) + def __xor__(self, object other): self._assert_cpu() return _compute_binary_op('bit_wise_xor', self, other) + def __rxor__(self, object other): + self._assert_cpu() + return _compute_binary_op('bit_wise_xor', other, self) + def __lshift__(self, object other): self._assert_cpu() return _compute_binary_op('shift_left_checked', self, other) + def __rlshift__(self, object other): + self._assert_cpu() + return _compute_binary_op('shift_left_checked', other, self) + def __rshift__(self, object other): self._assert_cpu() return _compute_binary_op('shift_right_checked', self, other) + def __rrshift__(self, object other): + self._assert_cpu() + return _compute_binary_op('shift_right_checked', other, self) + cdef _array_like_to_pandas(obj, options, types_mapper): cdef: diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi index b07f6a5e054..91e00d8c7b5 100644 --- a/python/pyarrow/scalar.pxi +++ b/python/pyarrow/scalar.pxi @@ -201,36 +201,66 @@ cdef class Scalar(_Weakrefable): def __add__(self, object other): return _compute_binary_op('add_checked', self, other) + def __radd__(self, object other): + return _compute_binary_op('add_checked', other, self) + def __truediv__(self, object other): return _compute_binary_op('divide_checked', self, other) + def __rtruediv__(self, object other): + return _compute_binary_op('divide_checked', other, self) + def __mul__(self, object other): return _compute_binary_op('multiply_checked', self, other) + def __rmul__(self, object other): + return _compute_binary_op('multiply_checked', other, self) + def __neg__(self): return _pc().call_function('negate_checked', [self]) def __pow__(self, object other): return _compute_binary_op('power_checked', self, other) + def __rpow__(self, object other): + return _compute_binary_op('power_checked', other, self) + def __sub__(self, object other): return _compute_binary_op('subtract_checked', self, other) + def __rsub__(self, object other): + return _compute_binary_op('subtract_checked', other, self) + def __and__(self, object other): return _compute_binary_op('bit_wise_and', self, other) + def __rand__(self, object other): + return _compute_binary_op('bit_wise_and', other, self) + def __or__(self, object other): return _compute_binary_op('bit_wise_or', self, other) + def __ror__(self, object other): + return _compute_binary_op('bit_wise_or', other, self) + def __xor__(self, object other): return _compute_binary_op('bit_wise_xor', self, other) + def __rxor__(self, object other): + return _compute_binary_op('bit_wise_xor', other, self) + def __lshift__(self, object other): return _compute_binary_op('shift_left_checked', self, other) + def __rlshift__(self, object other): + return _compute_binary_op('shift_left_checked', other, self) + def __rshift__(self, object other): return _compute_binary_op('shift_right_checked', self, other) + def __rrshift__(self, object other): + return _compute_binary_op('shift_right_checked', other, self) + _NULL = NA = None diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 7a4328ce9f9..d6dfc0e936a 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -4671,6 +4671,14 @@ def test_arithmetic_dunders(): assert (arr1 ** 2).equals(pc.power_checked(arr1, 2)) assert (arr1 - arr2).equals(pc.subtract_checked(arr1, arr2)) + # GH-55893: the Python scalar is the left operand. + arr = pa.array([3, None]) + assert (6 + arr).equals(pc.add_checked(6, arr)) + assert (6 - arr).equals(pc.subtract_checked(6, arr)) + assert (6 * arr).equals(pc.multiply_checked(6, arr)) + assert (6 / arr).equals(pc.divide_checked(6, arr)) + assert (6 ** arr).equals(pc.power_checked(6, arr)) + def test_bitwise_dunders(): # GH-32007 @@ -4683,6 +4691,14 @@ def test_bitwise_dunders(): assert (arr1 << arr2).equals(pc.shift_left_checked(arr1, arr2)) assert (arr1 >> arr2).equals(pc.shift_right_checked(arr1, arr2)) + # GH-55893: the Python scalar is the left operand. + arr = pa.array([3, None]) + assert (6 & arr).equals(pc.bit_wise_and(6, arr)) + assert (6 | arr).equals(pc.bit_wise_or(6, arr)) + assert (6 ^ arr).equals(pc.bit_wise_xor(6, arr)) + assert (6 << arr).equals(pc.shift_left_checked(6, arr)) + assert (6 >> arr).equals(pc.shift_right_checked(6, arr)) + def test_dunders_unmatching_types(): # GH-32007 @@ -4794,6 +4810,9 @@ def __radd__(self, other): # Python will fallback to a TypeError with pytest.raises(TypeError, match="unsupported operand type\\(s\\)"): op(pa.array([1, 2, 3]), object()) + # GH-55893: the reflected operation must also reject unknown operands. + with pytest.raises(TypeError, match="unsupported operand type\\(s\\)"): + op(object(), pa.array([3])) def test_arithmetic_dunder_raises_arrow_invalid(): diff --git a/python/pyarrow/tests/test_scalars.py b/python/pyarrow/tests/test_scalars.py index 0998f68de32..98470b5b5fc 100644 --- a/python/pyarrow/tests/test_scalars.py +++ b/python/pyarrow/tests/test_scalars.py @@ -1040,6 +1040,14 @@ def test_arithmetic_dunders(): assert (scl1 ** 2).equals(pc.power_checked(scl1, 2)) assert (scl1 - scl2).equals(pc.subtract_checked(scl1, scl2)) + # GH-55893: the Python scalar is the left operand. + scl = pa.scalar(3) + assert (6 + scl).equals(pc.add_checked(6, scl)) + assert (6 - scl).equals(pc.subtract_checked(6, scl)) + assert (6 * scl).equals(pc.multiply_checked(6, scl)) + assert (6 / scl).equals(pc.divide_checked(6, scl)) + assert (6 ** scl).equals(pc.power_checked(6, scl)) + def test_bitwise_dunders(): # GH-32007 @@ -1052,6 +1060,14 @@ def test_bitwise_dunders(): assert (scl2 << scl1).equals(pc.shift_left_checked(scl2, scl1)) assert (scl2 >> scl1).equals(pc.shift_right_checked(scl2, scl1)) + # GH-55893: the Python scalar is the left operand. + scl = pa.scalar(3) + assert (6 & scl).equals(pc.bit_wise_and(6, scl)) + assert (6 | scl).equals(pc.bit_wise_or(6, scl)) + assert (6 ^ scl).equals(pc.bit_wise_xor(6, scl)) + assert (6 << scl).equals(pc.shift_left_checked(6, scl)) + assert (6 >> scl).equals(pc.shift_right_checked(6, scl)) + def test_dunders_unmatching_types(): # GH-32007 @@ -1111,6 +1127,9 @@ def __radd__(self, other): # Python will fallback to a TypeError with pytest.raises(TypeError, match="unsupported operand type\\(s\\)"): op(pa.scalar(1), object()) + # GH-55893: the reflected operation must also reject unknown operands. + with pytest.raises(TypeError, match="unsupported operand type\\(s\\)"): + op(object(), pa.scalar(3)) def test_arithmetic_dunder_raises_arrow_invalid():