Skip to content

__array__ not called on ufunc out parameters #7158

@adler-j

Description

@adler-j

In the __array__ documentation it says:

If a class (ndarray subclass or not) having the __array__ method is used as the output object of an ufunc, results will be written to the object returned by __array__.

however, this does not seem to be the case, as this example shows:

import numpy as np

class NDLike(object):
    def __init__(self, values):
        self.values = np.asarray(values)
    def __array__(self):
        return self.values
    def __array_wrap__(self, result):
        return type(self)(result)
    def __repr__(self):
        return "[%s]\n%s" % (type(self), self.values)

    # Adding these make no difference
    def __array_prepare__(self, array, context=None):
        return array.values if isinstance(array, NDLike) else array
    __array_priority__ = 1000000.0

this works:

>>> x = NDLike([1, 2, 3])
>>> np.negative(x)
[<class '__main__.NDLike'>]
[-1 -2 -3]

while this doesnt:

>>> x = NDLike([1, 2, 3])
>>> out = NDLike([0, 0, 0])
>>> np.negative(x, out=out)
TypeError: return arrays must be of ArrayType

Clearly, there is no attempt to call __array__ on out.

It seems either the documentation is wrong, or the implementation is lacking.

Enviromenment: Windows 10, python 2.7.11, numpy 1.10.1

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