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
API: make piecewise subclass safe using use zeros_like. #18110
Conversation
@rgommers for clarification, how do you think about this type of change considering your proposed NEP? It is unlikely to break anyone's code (but not technically impossible), in this case it was probably intended (using Anyway, this makes sense to me, just needs a test. Release notes and possibly |
Nevermind, I guess we will just agree this is more of a bugfix, still wonder if |
|
c1c1b51
to
97ed51f
Compare
OK, I pushed a changelog entry, renaming the commit message. Obviously, if this does end up causing any trouble, do just revert it. But it does seem just to have been an oversight. p.s. With this change, it should work with |
How exactly does this change work? What does a simple |
@eric-wieser - when implementing Given the huge numpy API, this saves a lot of upfront work when creating a new subclass or duck type. It also means one immediately benefits from any bug fixes in those pieces of code, though of course at the price of the (relatively minor) hassle of fixing things when a version updates changes what happens (it just means covering all 262 wrapped function with tests...). To get a sense, see the list of "safe" functions at https://github.com/mhvk/astropy/blob/53c06ff40a57b2af577b7877306eaa8a462551a9/astropy/utils/masked/function_helpers.py#L84-L113 for my masked class, and at https://github.com/astropy/astropy/blob/53c06ff40a57b2af577b7877306eaa8a462551a9/astropy/units/quantity_helper/function_helpers.py#L62-L92 for Anyway, concretely with the PR, one more function moves to "safe" - which means I don't have to try to keep a separate copy of the code up to date with that at numpy (as is, probably astropy is already carrying bugs that were solved in numpy; eventually I guess we'd need to move to some type of introspection to get numpy function source code and then apply patches instead of just copying pieces). |
Ah, I forgot we had that fallback. Thanks! |
Hmm, thanks for bringing it up. My first angle was that even without the I am not sure we really define exactly, what happens when using None of this is ideal, but I expect we have so many other functions fitting into the "safe" category, that one more just doesn't make a difference when it comes to freedom of changing NumPy in the future and at the same time it does help right now. @mhvk one thing I just noticed glancing at the quantity |
@seberg - thanks! Indeed, the change would of course also be useful for subclasses that do not define On |
@mhvk yeah, it is a bit tricky, but mostly if a subclass doesn't define In theory, there is a general issue for what happens if you inherit. I actually believe that the best way may to have logic like:
The As to this PR, would it be possible to add a short MA (or similar) test? Otherwise I think this is just good to go? |
@mhvk this PR is waiting for a test |
Subclass input of piecewise was already respected, so it seems more logical to ensure the output is the same subclass (possibly just an oversight that it was not done before).
97ed51f
to
aeae93b
Compare
@mattip - thanks for the reminder; I added a test now! |
LGTM, thanks Marten and matti for the ping :). |
Arguably more logical and makes the otherwise very subclass-aware code subclass safe.
Mostly because as I am implementing a new
Masked
class that can handle subclasses properly, I found that for my__array_function__
helpers I had to copy the wholepiecewise
implementation to just make this single change.