Skip to content
Open
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
31 changes: 21 additions & 10 deletions python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member

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_function would not end up becoming NotImplemented?

Comment on lines +242 to +243
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
Loading