Skip to content

Comments

fix: defer Quantity __array_ufunc__ for duck types - swev-id: astropy__astropy-13977#85

Open
casey-brooks wants to merge 6 commits intoastropy__astropy-13977from
casey/feature-ufunc-notimplemented-duck
Open

fix: defer Quantity __array_ufunc__ for duck types - swev-id: astropy__astropy-13977#85
casey-brooks wants to merge 6 commits intoastropy__astropy-13977from
casey/feature-ufunc-notimplemented-duck

Conversation

@casey-brooks
Copy link

Summary

  • add a guard in Quantity.__array_ufunc__ to return NotImplemented when mixed operands include non-Quantity ducks exposing .unit while skipping table columns
  • convert mixed-duck converter failures into UnitTypeError and reuse duck deferral helper logic
  • extend duck interoperability tests to cover reflected operations and column interactions

Testing

  • .venv310/bin/pytest astropy/units -q

Closes #69

Casey Quinn and others added 6 commits October 18, 2025 19:56
…mpatible mixed binary calls\n\n- Catch converter discovery/application errors and return NotImplemented for mixed duck-type inputs per issue #8.\n- Add tests for deferral behavior and unchanged cases.\n- Document new deferral note in Quantity docs.\n\nRefs: swev-id: astropy__astropy-13977
…ufunc__ to catch AttributeError/TypeError for mixed duck-type deferral (binary ufuncs)
…res; clarify docs and helper comment to cover binary ufunc __call__ and note reduce/accumulate/at unchanged
@casey-brooks casey-brooks requested a review from a team December 18, 2025 15:19
@casey-brooks
Copy link
Author

Test & Lint Summary

  • .venv310/bin/pytest astropy/units -q
    • 2946 passed, 42 skipped, 5 xfailed

@rowan-stein
Copy link
Collaborator

Requesting review.

Summary:

  • Change in astropy/units/quantity.py: Quantity.array_ufunc now returns NotImplemented early when inputs include an incompatible duck type (object exposes .unit but is neither Quantity nor ndarray), allowing reflected array_ufunc to proceed.
  • Optional guarded converter handling to return NotImplemented for the same duck case on conversion failures, preserving existing behavior otherwise.
  • Tests added to ensure reflected duck operations work and astropy.table.Column interactions remain unchanged.

Focus:

  • Verify early guard correctness and that Column (ndarray subclass) behavior is unchanged.
  • Ensure fast path performance is not impacted.
  • Review tests for adequate coverage across add/mul/comparisons.

Closes #69.

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

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

  • Verified the new mixed-duck guard defers to foreign array_ufunc implementations without interfering with Quantity/Column or numpy inputs.
  • Checked converter fallback paths and error translation; behavior for incompatible Quantity pairs remains unchanged and ducks now cleanly get control.
  • Exercised the expanded test suite; coverage hits reflected dispatch, converter failures, numpy scalars/arrays, and Column interactions, and the docs summarize the new protocol.

@rowan-stein rowan-stein changed the title fix: defer Quantity __array_ufunc__ for duck types fix: defer Quantity __array_ufunc__ for duck types - swev-id: astropy__astropy-13977 Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants