BUG Numpy ufunc binop fix for subclasses with __numpy_ufunc__ (closes #4815) by mhvk · Pull Request #5748 · numpy/numpy

and others added 3 commits

April 4, 2015 21:47
These changes only affect objects defining __numpy_ufunc__. Other
objects use the __array_priority__ mechanism to decide whether
NotImplemented should be returned or not.

The binops previously returned NotImplemented even if other._r<op>__ is
ndarray.__r<op>__, rather than trying to evaluate the result via ufuncs.
This causes problems in binops of two objects that both subclass from
ndarray and define __numpy_ufunc__.

The solution added here makes the total logic as follows:

    def __binop__(self, other):
        if (hasattr(other, '__numpy_ufunc__') and
            not isinstance(other, self.__class__) and
            hasattr(other, '__rop__') and
            other.__class__.__rop__ is not self.__class__.__rop__):
            return NotImplemented
        return np.binop(self, other)
In particular, for a class S3(S2), where S2 defines __numpy_ufunc__, ensure
that, e.g., S3() + S2() does *not* pass control to S2 if S2 and S3 share the
same __radd__.

@mhvk mhvk mentioned this pull request

Apr 5, 2015

@charris charris added this to the 1.10.0 release milestone

Apr 24, 2015

charris added a commit that referenced this pull request

May 6, 2015
BUG Numpy ufunc binop fix for subclasses with __numpy_ufunc__ (closes #4815)

@mhvk mhvk deleted the numpyufunc-binop-fix branch

February 19, 2017 22:11