Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The __array_function__ protocol for overriding NumPy's API #790

Closed
shoyer opened this issue Apr 16, 2019 · 2 comments · Fixed by #905
Closed

The __array_function__ protocol for overriding NumPy's API #790

shoyer opened this issue Apr 16, 2019 · 2 comments · Fixed by #905

Comments

@shoyer
Copy link

shoyer commented Apr 16, 2019

We have been drafting a new protocol to allow for overriding NumPy functions, e.g., for np.concatenate: http://www.numpy.org/neps/nep-0018-array-function-protocol.html

It would be great to experiment with getting this working in pint, since arrays with units are one of the intended use-cases.

@hgrecco
Copy link
Owner

hgrecco commented Apr 17, 2019

Thanks for writing. I have been looking forward to see something like this implemented in numpy and we have indeed played with the array function protocol. See #753 (and cc: @andrewgsavage )

I was wondering if you can clarify something that is not clear to me. Will this solve the asanyarray issue or we will still need to subclass? AFAIK NumPy functions do not like duck typing. You MUST derive from ndarrary to make some functions work because np.asanyarray() which is called frequently only allows only strict subclasses.

@shoyer
Copy link
Author

shoyer commented Apr 17, 2019

AFAIK NumPy functions do not like duck typing. You MUST derive from ndarrary to make some functions work because np.asanyarray() which is called frequently only allows only strict subclasses.

It is arguably worse than this -- many NumPy functions (e.g., np.concatenate()) use np.asarray() internally, which converts subclasses into base-class instances. This is mostly a historical artifact, because NumPy comes with some non-substitutable subclasses (namely, MaskedArray and np.matrix).

__array_function__ is intended to work without subclassing, but you do need to define operations with units for all numpy array functions you'll want to support. Fortunately this usually be done in a fairly mindless way.

@bors bors bot closed this as completed in #905 Dec 11, 2019
@bors bors bot closed this as completed in 43fbae2 Dec 11, 2019
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 a pull request may close this issue.

2 participants