-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG __numpy_ufunc__ gives unexpected TypeError with subclasses #4815
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
Conversation
This is due to the behavior exception in
|
@pv - that seems troublesome! I guess the reason for the trouble in part seems to be that the code thinks
Could the problem be that in numbers.c, what is done is
EDIT: the same holds for the objects rather than the class: |
The problem here is that Checking for However, checking for
Note that apart from the NB. there are a number of other related open issues, which can give perspective on what people regard as "expected" behavior. Implementation such as |
Hmm, I fear my example may not be that great -- one might have to traverse the whole MRO to see whether the reverse operation is defined in a subclass. One possible way out would be to check whether the reverse operation of the RHS refers to the same function as that on the LHS, i.e., only hand over to ... OK, I see you reached the same conclusion. I guess the question is whether one could do the equivalent of the |
Do you remember the details of why we added the special case dispatch to
|
Actually, if I understand correctly, the whole thing is needed in the first place to ensure that if the RHS has
|
@njsmith: yes, if we feel that I don't immediately recall what will break if this mechanism is simply ripped out and my PR gh-3856 is reverted. Potential points of failure: (i) operations with classes that don't define (The related other issues: gh-3812, gh-3856, gh-3375, gh-4766, gh-3502, gh-4709) |
The Python binop mechanism still has priority over the numpy one in the I agree that the more important issue is compatibility. The above reasoning Of course we aren't in this that world. But I was surprised at your comment As a straw man, which cases would break if we say (1) if the other operand On Thu, Jun 19, 2014 at 4:54 PM, Pauli Virtanen notifications@github.com
Nathaniel J. Smith |
@njsmith - to add to the complications, one should also consider the case where |
@pv, @njsmith - as I mentioned above, at least my problem gets solved if the handover to |
For completeness: Even with my changes, I'm still bitten by #4766 EDIT: being bitten is not for my example above, but for astropy/astropy#2583 |
FYI, the change to let pass on to Not quite sure what to think about the larger question, though I'm wondering similarly to @njsmith whether it makes sense to let priorities over which object gets the call depend on whether |
One aspect that should be considered is that
needs to call The suggestion by @mhvk of using The current
A core question seems to be that if
mean? Does the LHS get to decide? Or is there a commutative decision rule (as in array_priority)? Or should the decision be semi-commutative (as in @mhvk suggestion) in the sense that if |
@pv - in your example, I would think that the sequence would be as follows: Here, in principle, But obviously it causes the problems that led to this issue... I'm not quite sure why you think there is a problem with my approach in general: essentially, if the first class also has I do worry about your specific example of multiplication having different meaning. E.g., suppose I multiply an astropy |
@mhvk: yes, this issue was considered earlier when adding the The problem with the approach your propose is that it will dispatch to I think the responsibility of dealing with this problem lies with the binary op layer, and not the ufunc layer, because the decision is about what operation The problem is also not fully related to It is in principle possible to add informative markers such as |
@pv - OK, that makes sense. I agree that this really has to be done at the operator level: |
The change in this PR as it is now makes I think, unless we want to simply use the |
Here's one suggestion: https://github.com/pv/numpy/compare/numpyufunc-binop-subclass-fix At first sight this may look like it's munging with CPython implementation details, but the added checks can be expressed also as Do we tolerate this? Does this open other cans of worms? |
The ufunc override `__numpy_ufunc__` still has a few unresolved issues regarding its behavior towards Python operators see numpygh-4815. To avoid releasing numpy with an interface that might change in the next numpy version and to not further delay the 1.9 release it has been decided to disable the feature in 1.9.0.
Changed this to 1.10 blocker as |
@pv - as I've restarted working on |
@pv - it would be good if we could solve this issue for assigning which operand gets to use its |
@pv, @njsmith - what would you recommend as the path forward on this issue? For astropy's |
Checking 1.10 blockers brought up by my |
@pv, @njsmith - still wondering how to move this forward with this
In the end, both try to express the cases where RHS should be able to override normal handling but which are not captured by the normal |
@mhvk @pv As I see it, Pauli addresses the python interpreter operator problems at the operator level, which I think is were it belongs. I'm inclined to close this and go with #5748. Is it correct that ufuncs still have problems? I note in trying astorpy.units that |
@charris - yes, fine to go with #5748; I'll close this PR. It is also fine that |
BUG Numpy ufunc binop fix for subclasses with __numpy_ufunc__ (closes #4815)
I'm trying to use
__numpy_ufunc__
for astropyQuantity
and found surprising difficulties with subclasses, where<Q-subclass>+<Q>
yields aTypeError
while<Q>+<Q-subclass>
works (see below for a simple example using justndarray
subclasses). This behaviour seems surprising. Am I missing something?