-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
NumPy ignores __array_priority__ when the right-hand-side operand is a subclass of ndarray #4766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Unfortunately, there is the second problem (as explained in gh-4709) that Python's operator override system has difficulties with the case superclass + two subclasses --- ie. what should the superclass In principle, this problem has nothing to do with |
Does numpy_ufunc solve your original problem that prompted the bug
|
@njsmith: it will probably solve it, but the execution will end up calling it, due to the aggressive implementation of the base class binop |
@pv: Actually, I believe that changing Anyhow, using Thanks |
The following code does not output "sub_class radd" as expected since NumPy ignores
__array_priority__
when the right-hand-side operand is a subclass of ndarray.The problem is line 282 in number.c where a branch uses
PyArray_Check()
to check the right-hand-side operandm2
. IfPyArray_Check(m2)
returnsTrue
, which is the case whenm2
is a subclass of ndarray,__array_priority__
is ignored.Now, this is not a problem when the left-hand-side operand is an ndarray since in CPython a subclass always takes precedence over its superclass. However, a NumPy scalar is not a sub-/super-class of ndarray as the example code above illustrates.
There is a very simple fix: change
PyArray_Check(m2)
toPyArray_CheckExact(m2)
. However, this fix has the side-effect that the operation will fail with NotImplementedError if the subclass doesn’t implement the operator, which is a change to the current behavior.The text was updated successfully, but these errors were encountered: