8000 BUG __numpy_ufunc__ does not properly deal with NotImplemented · Issue #5074 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG __numpy_ufunc__ does not properly deal with NotImplemented #5074
Closed
@mhvk

Description

@mhvk

In continuing to try to get astropy's Quantity to behave with __numpy_ufunc__, I found what probably is another bug. One of our tests checks that an integer can be used as an __index__ to, e.g., multiply a list, as in:

import numpy as np
np.array([2]) * ['a', 'b']

(which yields ['a', 'b', 'a', 'b']).

But if I make a trivial class that implements __numpy_ufunc__, the NotImplemented does not seem to be passed on correctly:

class MyA(np.ndarray):
    def __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs):
        result = getattr(ufunc, method)(*((input.view(np.ndarray)
                                           if isinstance(input, np.ndarray)
                                           else input) for input in inputs),
                                        **kwargs)
        print("Got result={}".format(result))
        return result

np.array([2]).view(MyA) * ['a', 'b']

yields

Got result=NotImplemented
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-b1e259840c66> in <module>()
----> 1 np.array([2]).view(MyA) * ['a', 'b']

TypeError: __numpy_ufunc__ not implemented for this type.

p.s. This may be related to #4815

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0