GH-49826: [Python] Return NotImplemented from Scalar/Array arithmetic dunders for unsupported types#49845
Open
alex-anast wants to merge 2 commits intoapache:mainfrom
Conversation
… dunders for unsupported types
…dunders for unsupported types
|
|
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
In pyarrow 24.0.0,
ScalarandArraygained arithmetic dunder methods (#32007) that unconditionally dispatch topyarrow.compute.call_function. When the other operand is an unrecognized type,_pack_compute_argsraisesTypeErrorinstead of returningNotImplemented. This prevents Python from falling back to the other operand's reflected methods (__radd__,__rsub__, etc.), breaking downstream libraries that relied on this protocol.What changes are included in this PR?
_compute_binary_ophelper inscalar.pxithat wrapscall_functionin atry/except TypeErrorand returnsNotImplementedon failure. This follows the same pattern already used byScalar.__eq__andArray.__eq__.Scalar(scalar.pxi) andArray(array.pxi) to use this helper.test_scalars.pyandtest_array.pyverifying that reflected operators on custom types are correctly invoked.Are these changes tested?
Yes. New parametrized tests (
test_dunders_return_notimplemented_for_unknown_types) cover all 10 binary operators for bothScalarandArray. The bug was also manually reproduced against pyarrow 24.0.0 to confirm the tests exercise the right code path.Are there any user-facing changes?
Yes.
ScalarandArrayarithmetic dunders now returnNotImplementedinstead of raisingTypeErrorwhen the other operand is not a recognized Arrow/NumPy type. This restores the pre-24.0.0 behavior where Python would fall back to the other operand's reflected method.Closes [Python] Scalar arithmetic dunders raise TypeError instead of returning NotImplemented #49826