-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Closed
Milestone
Description
With NumPy master, it is currently impossible to override ndarray @ other if other also implements __array_ufunc__.
Consider:
import numpy as np
np.__version__ # '1.13.0.dev0+9ef5891'
class OptOut:
__array_ufunc__ = None
def __rmatmul__(self, other):
return 'rmatmul'
class OtherArray:
def __array_ufunc__(self, *args, **kwargs):
return 'array_ufunc'
def __rmatmul__(self, other):
return 'rmatmul'
array = np.arange(3)
opt_out = OptOut()
other_array = OtherArray()
# OptOut works as expected:
array * opt_out # TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'OptOut'
array @ opt_out # 'rmatmul'
# But OtherArray does not:
array * other_array # 'array_ufunc'
array @ other_array # TypeError: Object arrays are not currently supportedThis is problematic. We definitely need a way to allow for overriding @ from the second argument, whether than means breaking the rules on __array_ufunc__ (allowing matmul to be passed even though it isn't a ufunc) or breaking the rules for __matmul__ (to call __rmatmul__ or allow __rmatmul__ to be called by Python, even when __array_ufunc__ is implemented on the other argument).
rzyu45