Skip to content

BUG: repr() broken on ndarray subclasses that define __array_function__ unless they support np.array_repr() #12162

Closed
@shoyer

Description

@shoyer

The problem appears to be that ndarray.__repr__ is mapped internally to call np.array_repr():

In [1]: import numpy as np

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

In [3]: repr(np.array(1).view(Sub))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-d6ff958f4fcf> in <module>()
----> 1 repr(np.array(1).view(Sub))

~/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         return public_api
    153

~/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 array_repr at 0x105692e18> on types that implement __array_function__: [<class '__main__.Sub'>]

This is a somewhat poor user experience. We should map ndarray.__repr__ to np.array_repr.__wrapped__ instead.

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