Skip to content

Allow subclasses to preserve their types during all binary ops and ufuncs - #11985

Open
Sevans711 wants to merge 2 commits into
pydata:mainfrom
Sevans711:fix/subclasses-can-preserve-types-in-all-binary-ops
Open

Sevans711 wants to merge 2 commits into
pydata:mainfrom
Sevans711:fix/subclasses-can-preserve-types-in-all-binary-ops

Conversation

@Sevans711

Copy link
Copy Markdown

Description

See #11829 for detailed description. Basically, when subclassing from xarray, it was previously impossible for subclasses to maintain their types in these three cases:

  1. xr.Dataset * CustomDataArray, where * can be any binary operation (e.g. multiplication, addition, subtraction)
  2. ufunc(xr.Dataset, CustomDataArray), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)
  3. ufunc(xr.DataArray, CustomDataset), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)

This PR fixes that by adding hooks into Dataset._binary_op and SupportsArithmetic.__array_ufunc__, which check whether _binary_op and __array_ufunc__ have been overridden by the other object. If they have been overridden there (and not by self, i.e. self isn't also a subclass; this was necessary to avoid giving NotImplemented in too many cases) then they return NotImplemented, which allows the subclass to implement its own logic for handling the operations.

This change may start causing TypeError on subclasses which override __array_ufunc__ or _binary_op. But, the reason I decided to go with "does it override" instead of a custom flag as originally proposed in #11829, was to mimic builtin python logic for dunder method and to avoid the need for xarray to start maintaining a flag with a custom name. I suspect this "cleaner" solution's reduced long-term maintenance cost will outweigh any friction caused by this breakage, especially because xarray clarifies that subclassing likely requires poking around with internals which may change at any time and without notice.

The new test file test_subclassing.py provides a minimal implementation of a subclass which maintains its type throughout binary operations, and ensures all possible binary combinations of DataArrays/Datasets, with at least one of the objects being an xarray subclass, can actually return the subclass's type. This includes covering all three cases above.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR. Tools: Claude (Opus 5.5) helped me discover the "only give NotImplemented if self is also not a subclass" part of the implementation, consider whether a flag implementation or "is the method overridden?" would be a better design, and sanity checked some of the implementation details. I also incorporated Github Copilot inline coding suggestions along the way, though I did wrote most of the Foo subclass implementations here initially without AI. I wrote this PR message without AI as well.

cc: @keewis, @jsignell, as we had a conversation about #11829 a couple days ago.

This branch has not been deployed

No deployments
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.

Subclasses cannot preserve their types during some binary ops and ufuncs combining DataArray and Dataset

1 participant