Repository navigation
ENH: Allow ndarray.__array_function__ to dispatch functions that use like= - #27504
Conversation
91bebea to
a1cbd6d
Compare
seberg
left a comment
There was a problem hiding this comment.
Thanks, LGTM. I would like to consider briefly once more not what should happen for subclasses that do implement __array_function__ but rather subclasses that do not.
like= was always designed for those that do, so we didn't pay much attention about what the right thing is for those that do not implement it. In that sense, maybe it would be reasonable if we do actually call self.__array_wrap__() already in __array_function__.
OTOH, that may be good to have a release note for, since it subtly changes behavior (unlikely to be noticed much, though).
Subclasses that do not want the array-wrap behavior, will have to view a base-class array or call the original function themself (although that may or may not wrap depending on the function).
I can go either way, but my gut leans a very tiny bit towards wrapping right now.
|
@seberg - It took me some wondering but I ended up thinking there is no need for wrapping, at least not inside numpy/numpy/_core/src/multiarray/arrayfunction_override.c Lines 276 to 283 in ff88e0c (Or did you mean overriding it there?) FWIW, in the regular case of dispatching on some array-like argument, numpy/numpy/_core/src/multiarray/arrayfunction_override.c Lines 554 to 571 in ff88e0c (I don't think you meant to add wrapping for those, but if we wanted that, it would need some checking of the python code, to remove wrapping there...) p.s. For subclasses that do define |
|
Ah, good point. So right now it actually doesn't matter to begin with, although we may want to change that at some point, I suppose. At some point we might not reject the |
seberg
left a comment
There was a problem hiding this comment.
Sorry for not going back to this earlier. Let's give this a try, thanks!
ndarray.__array_function__ to dispatch functions that use like=
|
Arg, so here is the thing that I guess still makes me pause: So to me, it seems like it would be a fix to do what you said: Pass on Basically, there is the weird thing that for other functions we often (but not always!) preserve subclasses, but for creation functions OTOH, I am not 100% sure either that it is doing much of a favor to preserve the subclasses here if those subclasses don't bother implementing TL;DR: I still feel it seems more right to return subclasses, but I am also fine with putting this in, it doesn't break anything (although it might slightly cement the behavior). |
|
@seberg - it remains a tricky one. On the one hand, I think that the whole But maybe more importantly is that it is tricky, since if we do it for the Thinking concretely for the case of astropy, it would not help the implementations to have a subclass returned, as we will regardless do things like set units, etc., after this call happens. Also, assuming this really should be done generally, not all functions should return subclasses (e.g., for quantities, So, overall perhaps best to just take this as a bug fix that allows using the |
|
Sorry, one further thought: in principle, I think it would be OK for the actual C code that creates arrays to know how to create a subclass, and then |
|
Yeah, it isn't clear cut. Because I can also say that most subclasses that do not implement And if they interact gracefully, then it actually makes sense to just return a plain array! So maybe, I should just stop worry about it, either way someone who tries to be lazy by not implementing these (falling back), will have to do more than they hope. And while I may have a slight tendency to the other being the better default... In the end, it is just a default! So... Let's give this a shot, I don't think anyone else will chime in after all. |
This makes
ndarray.__array_function__look for_implementation, but dispatch to the function if that is not found.This is rather crude, as there are no further sanity checks, but I think it is OK to let subclasses deal with possible problems.
A more general question is whether we should not let
ndarray.__array_function__actually return a subclass in this case. I guess this is possible in principle, by actually using theselfargument in the method implementation, and passing that on toarray_function_method_impl.EDIT: rebased, so now just on its own
Note: built on #27503, so just look at second commit.fixes #27500