Skip to content

GH-55893: [Python] Add reflected arithmetic operators to Scalar and Array - #55949

Open
dhaanish597 wants to merge 3 commits into
apache:mainfrom
dhaanish597:fix-pyarrow-reflected-arithmetic
Open

dhaanish597 wants to merge 3 commits into
apache:mainfrom
dhaanish597:fix-pyarrow-reflected-arithmetic

Conversation

@dhaanish597

@dhaanish597 dhaanish597 commented Dec 3, 2026 •

Copy link
Copy Markdown

Rationale for this change

PyArrow scalars and arrays support operations such as x + 2, but the equivalent expression 2 + x raises TypeError.

What changes are included in this PR?

Add the ten reflected operators listed in the issue to Scalar and Array, with matching type annotations. Dispatch to the existing compute kernels with operands in their original order, including for subtraction, division, powers, and shifts.

Are these changes tested?

On a native macOS arm64 build of the pinned source: 22 new regression tests and 24 existing arithmetic tests passed. The focused stub runtime test passed, and mypy found no issues in 26 source files. The full suite was not run locally.

Are there any user-facing changes?

Python numbers can now appear on the left of the supported operators. Unsupported operand types still raise TypeError.

Was AI used for this PR?

OpenAI ChatGPT (Dot) assisted with reproduction and generated the implementation, tests, type-stub changes, and PR draft. The contributor personally reviewed the patch before submission.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

(operator.or_, 7), (operator.xor, 5), (operator.lshift, 48),
(operator.rshift, 0),
])
def test_reflected_arithmetic(factory, operation, expected):

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.

@dhaanish597 any special reason that you created a separate new file for the test?
See the initial implementation of arithmetic on arrays and scalars and try to follow the same style #48085
a19f13b

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Tadeja! I moved the tests into test_array.py and test_scalars.py to match the original PR. Both files passed locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants