Repository navigation
Conversation
This branch has not been deployed
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.
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:
xr.Dataset * CustomDataArray, where*can be any binary operation (e.g. multiplication, addition, subtraction)ufunc(xr.Dataset, CustomDataArray), where ufunc can be any numpy binary ufunc (e.g. np.minimum, np.divmod)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_opandSupportsArithmetic.__array_ufunc__, which check whether_binary_opand__array_ufunc__have been overridden by theotherobject. If they have been overridden there (and not byself, i.e.selfisn'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.pyprovides 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
whats-new.rstapi.rstAI Disclosure
cc: @keewis, @jsignell, as we had a conversation about #11829 a couple days ago.