GH-49826: [Python] Return NotImplemented from Scalar/Array arithmetic dunders for unsupported types#49845
Conversation
… dunders for unsupported types
…dunders for unsupported types
|
|
|
|
|
|
1 similar comment
|
|
|
@AlenkaF apologies, hadn't seen the other PR. I think there is something sketchy going on there, though, if you look at the user's profile, with e.g. ~280 git commits in a day, it might be that these PRs are fully auto-generated. This one covers Let me know if I should keep this PR or not. |
All good, no harm done ;)
Yeah, lots of sketchy things going on in these days :) Happy to see a normal response on this PR!
This is great, thanks! Will have a look tomorrow.
Yes, please do keep it. If there is no response on the other PR I plan to proceed with yours. |
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.AI-generated code disclosure
This PR was developed with assistance from an AI coding tool (Claude, Anthropic). All changes have been reviewed, understood, and verified.
Closes [Python] Scalar arithmetic dunders raise TypeError instead of returning NotImplemented #49826