Skip to content

MAINT: improved error message when no __array_function__ implementation found #12251

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

Merged
merged 4 commits into from
Oct 25, 2018

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Oct 23, 2018

Fixes #12213

Before:

In [1]: import numpy as np

In [2]: class MyArray:
   ...:     def __array_function__(*args, **kwargs):
   ...:         return NotImplemented
   ...:

In [3]: np.sum(MyArray())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-c8a80bb1d37e> in <module>()
----> 1 np.sum(MyArray())

~/dev/numpy/numpy/core/overrides.py in public_api(*args, **kwargs)
    149             relevant_args = dispatcher(*args, **kwargs)
    150             return array_function_implementation_or_override(
--> 151                 implementation, public_api, relevant_args, args, kwargs)
    152
    153         # TODO: remove this when we drop Python 2 support (functools.wraps

~/dev/numpy/numpy/core/overrides.py in array_function_implementation_or_override(implementation, public_api, relevant_args, args, kwargs)
    108     raise TypeError('no implementation found for {} on types that implement '
    109                     '__array_function__: {}'
--> 110                     .format(public_api, list(map(type, overloaded_args))))
    111
    112

TypeError: no implementation found for <function sum at 0x10e070bf8> on types that implement __array_function__: [<class '__main__.MyArray'>]

After:

In [3]: np.sum(MyArray())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-c8a80bb1d37e> in <module>()
----> 1 np.sum(MyArray())

~/dev/numpy/numpy/core/overrides.py in public_api(*args, **kwargs)
    149             relevant_args = dispatcher(*args, **kwargs)
    150             return array_function_implementation_or_override(
--> 151                 implementation, public_api, relevant_args, args, kwargs)
    152
    153         # TODO: remove this when we drop Python 2 support (functools.wraps

~/dev/numpy/numpy/core/overrides.py in array_function_implementation_or_override(implementation, public_api, relevant_args, args, kwargs)
    108     raise TypeError('no implementation found for {} on types that implement '
    109                     '__array_function__: {}'
--> 110                     .format(public_api, list(map(type, overloaded_args))))
    111
    112

TypeError: no implementation found for 'numpy.sum' on types that implement __array_function__: [<class '__main__.MyArray'>]

@ahaldane
Copy link
Member

Seems like nearly essential addition to me, eg so we can distinguish np.equal from np.char.equal.

Ready to merge?

@shoyer
Copy link
Member Author

shoyer commented Oct 25, 2018

I think this is ready.

Another bonus is that it this gives us a better repr for functions in IPython, since they will now be displayed with the preferred high level module name, e.g., <function 'numpy.sum'> vs the current <function 'numpy.core.fromnumeric.sum'>.

@ahaldane
Copy link
Member

Yeah, I saw that and it's a great improvement. Maybe it actually deserves a release note for 1.16 in the improvements section? If you want to add it before merging I'd be happy to include it.

@shoyer
Copy link
Member Author

shoyer commented Oct 25, 2018

I added a release note, along with a few tests to spot-check __module__ updates and function overrides on NumPy functions.

@ahaldane
Copy link
Member

Looks good. Merging then, thanks Stephan.

@ahaldane ahaldane merged commit 872372b into numpy:master Oct 25, 2018
@charris charris changed the title MAINT: improved error message when no __array_function__ implementation found MAINT: improved error message when no __array_function__ implementation found Nov 10, 2018
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