Skip to content

__array_function__ errors should more clearly identify the non-implemented function #12213

Closed
@shoyer

Description

@shoyer

xref #12028

Here's what you currently see if __array_function__ returns NotImplemented:

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'>]

This error message should look something like this instead: TypeError: no implementation found for 'numpy.sum' on types that implement __array_function__: [<class '__main__.MyArray'>]

I think we will need to add a name parameter to array_function_override to do this properly. The best we could hope for with introspection is to use __module__ and __name__ to come up with something like numpy.core.fromnumeric.sum. This would be better what we currently have and could be a reasonable default, but we really don't want people reaching directly into internal modules like fromnumeric.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions