Skip to content
Open
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
7 changes: 4 additions & 3 deletions python/pyarrow-stubs-tests/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
10 changes: 10 additions & 0 deletions python/pyarrow-stubs/__lib/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions python/pyarrow-stubs/__lib/scalar.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down
19 changes: 19 additions & 0 deletions python/pyarrow/tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down