Skip to content

Conversation

mattip
Copy link
Member

@mattip mattip commented Sep 25, 2025

Closes #29804, related to # #17192, #29561 and maybe more

Users naively add a where argument without a proper out argument, and are surprised when the False values of the where mask are uninitialized. This PR adds a warning. The warning can be surpressed by using where=mask, out=None. That came in handy in some obscure tests.

@mattip mattip added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Sep 25, 2025
@mattip mattip changed the title add warning when calling ufunc with 'where' and without 'out' ENH: add warning when calling ufunc with 'where' and without 'out' Sep 25, 2025
@mattip mattip removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Sep 25, 2025
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I assume this is rare enough that introducing a warning is not a problem and it is very simple to remove the warning after all.

So I am in favor of trying this, approving, but the comments should be (mostly) addressed.

Test changes look good, the only ones I would worry about would be __array_wrap__ related, and those don't seem touched.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, nice; we really should have done this earlier...

mattip and others added 2 commits September 26, 2025 07:49
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! (I think my last comment was wrong, though)

@andyfaff
Copy link
Member

Is it possible to add a stacklevel value to the warning, because it'd be nice to see the warnings caller.

@seberg
Copy link
Member

seberg commented Sep 28, 2025

The warning already has the best possible stacklevel since it's in C (so no need to add one). But of course sometimes ufuncs are called indirectly (e.g. mean/var maybe).

I am not sure if there is a nice way to add skip_file_prefixes in C, although I guess one could bother to just call into Python to do it (warnings are slow anyway, I guess).
(This would also have to be Python 3.12 only as it is a new feature)

bwhitt7 pushed a commit to bwhitt7/numpy that referenced this pull request Oct 7, 2025
…umpy#29813)

* add warning when calling ufunc with 'where' and without 'out'

* DOC: add a release note

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Weird memory corruption bug related to np.divide

4 participants