Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ufuncs on object arrays should call __array_ufunc__ if it exists on the wrapped objects #15479

Copy link
Copy link
Open
@shoyer

Description

@shoyer
Issue body actions

Reproducing code example:

Here's my dummy class that implement __array_ufunc__ for ufuncs and arithmetic:

import numpy as np

class Wrapper(np.lib.mixins.NDArrayOperatorsMixin):
  def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
    return 'yes'

It works fine on its own, and scalar arithmetic works fine when wrapped in a dtype=object numpy.array. But calling ufuncs on the object array raises a strange TypeError:

print(Wrapper() + 1)  # 'yes'
print(np.sqrt(Wrapper()))  # 'yes'
print(np.array(Wrapper()) + 1)  # 'yes'
print(np.sqrt(np.array(Wrapper())))  # errors

Error message:

AttributeError                            Traceback (most recent call last)
AttributeError: 'Wrapper' object has no attribute 'sqrt'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-59-ace91090b167> in <module>()
      8 print(np.sqrt(Wrapper()))  # 'yes'
      9 print(np.array(Wrapper()) + 1)  # 'yes'
---> 10 print(np.sqrt(np.array(Wrapper())))  # errors

TypeError: loop of ufunc does not support argument 0 of type Wrapper which has no callable sqrt method

I'm not sure what exactly is going on, but it seems that we don't call actual ufuncs on the elements of dtype=object arrays, and instead only look for methods.

Numpy/Python version information:

1.17.5 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.