From 2da905431b6ce171e756813e51012df42c42d37f Mon Sep 17 00:00:00 2001 From: Dhaanish Ahamed B <165251528+dhaanish597@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:17:51 +0530 Subject: [PATCH 1/3] GH-55893: Add reflected arithmetic to PyArrow scalars and arrays --- python/pyarrow-stubs-tests/test_scalar.py | 7 ++-- python/pyarrow-stubs/__lib/array.pyi | 10 +++++ python/pyarrow-stubs/__lib/scalar.pyi | 10 +++++ python/pyarrow/array.pxi | 40 +++++++++++++++++++ python/pyarrow/scalar.pxi | 30 ++++++++++++++ .../tests/test_reflected_arithmetic.py | 26 ++++++++++++ 6 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 python/pyarrow/tests/test_reflected_arithmetic.py 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_reflected_arithmetic.py b/python/pyarrow/tests/test_reflected_arithmetic.py new file mode 100644 index 00000000000..9ad7b735d23 --- /dev/null +++ b/python/pyarrow/tests/test_reflected_arithmetic.py @@ -0,0 +1,26 @@ +import operator + +import pyarrow as pa +import pytest + + +@pytest.mark.parametrize("factory", [pa.scalar, lambda x: pa.array([x, None])]) +@pytest.mark.parametrize(("operation", "expected"), [ + (operator.add, 9), (operator.sub, 3), (operator.mul, 18), + (operator.truediv, 2), (operator.pow, 216), (operator.and_, 2), + (operator.or_, 7), (operator.xor, 5), (operator.lshift, 48), + (operator.rshift, 0), +]) +def test_reflected_arithmetic(factory, operation, expected): + operand = factory(3) + result = operation(6, operand) + if isinstance(operand, pa.Array): + assert result.equals(pa.array([expected, None])) + else: + assert result.equals(pa.scalar(expected)) + + +@pytest.mark.parametrize("operand", [pa.scalar(3), pa.array([3])]) +def test_unsupported_reflected_arithmetic(operand): + with pytest.raises(TypeError, match="unsupported operand"): + operator.add(object(), operand) From 2aa4a3379745e34f8fb3d41725351b60ba9f3b67 Mon Sep 17 00:00:00 2001 From: Dhaanish Ahamed B <165251528+dhaanish597@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:51:09 +0530 Subject: [PATCH 2/3] GH-55893: Add the ASF license header to the regression tests --- .../pyarrow/tests/test_reflected_arithmetic.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/pyarrow/tests/test_reflected_arithmetic.py b/python/pyarrow/tests/test_reflected_arithmetic.py index 9ad7b735d23..34a7c71d0ec 100644 --- a/python/pyarrow/tests/test_reflected_arithmetic.py +++ b/python/pyarrow/tests/test_reflected_arithmetic.py @@ -1,3 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + import operator import pyarrow as pa From 03d398810c07ca3d172cc2504d39ec0fd6339ba2 Mon Sep 17 00:00:00 2001 From: Dhaanish <165251528+dhaanish597@users.noreply.github.com> Date: Thu, 3 Dec 2026 20:17:46 +0530 Subject: [PATCH 3/3] GH-55893: Place reflected arithmetic checks in the existing tests --- python/pyarrow/tests/test_array.py | 19 ++++++++ .../tests/test_reflected_arithmetic.py | 43 ------------------- python/pyarrow/tests/test_scalars.py | 19 ++++++++ 3 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 python/pyarrow/tests/test_reflected_arithmetic.py 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_reflected_arithmetic.py b/python/pyarrow/tests/test_reflected_arithmetic.py deleted file mode 100644 index 34a7c71d0ec..00000000000 --- a/python/pyarrow/tests/test_reflected_arithmetic.py +++ /dev/null @@ -1,43 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -import operator - -import pyarrow as pa -import pytest - - -@pytest.mark.parametrize("factory", [pa.scalar, lambda x: pa.array([x, None])]) -@pytest.mark.parametrize(("operation", "expected"), [ - (operator.add, 9), (operator.sub, 3), (operator.mul, 18), - (operator.truediv, 2), (operator.pow, 216), (operator.and_, 2), - (operator.or_, 7), (operator.xor, 5), (operator.lshift, 48), - (operator.rshift, 0), -]) -def test_reflected_arithmetic(factory, operation, expected): - operand = factory(3) - result = operation(6, operand) - if isinstance(operand, pa.Array): - assert result.equals(pa.array([expected, None])) - else: - assert result.equals(pa.scalar(expected)) - - -@pytest.mark.parametrize("operand", [pa.scalar(3), pa.array([3])]) -def test_unsupported_reflected_arithmetic(operand): - with pytest.raises(TypeError, match="unsupported operand"): - operator.add(object(), operand) 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():