Skip to content

BUG: raise TypeError for in-place string multiply via arithmetic operator #29056

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

Closed
wants to merge 1 commit into from

Conversation

ngoldbaum
Copy link
Member

Fixes #29011.

I could add bounds checking to the loops themselves too, if people think it's worth adding that defensively.

@@ -16,7 +16,7 @@
MAR_M_dt64: MaskedArray[np.datetime64] = np.ma.MaskedArray([np.datetime64(1, "D")])
MAR_S: MaskedArray[np.bytes_] = np.ma.MaskedArray([b'foo'], dtype=np.bytes_)
MAR_U: MaskedArray[np.str_] = np.ma.MaskedArray(['foo'], dtype=np.str_)
MAR_T = cast(np.ma.MaskedArray[Any, np.dtypes.StringDType], np.ma.MaskedArray(["a"], "T"))
MAR_T = cast(np.ma.MaskedArray[Any, np.dtypes.StringDType], np.ma.MaskedArray(["a"], dtype="T"))
Copy link
Member Author

Choose a reason for hiding this comment

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

I happened to notice that this was passing "T" as the mask, so the resulting array had dtype='U1'. Perhaps not great that MaskedArray doesn't object if you pass mask=some_string!

Copy link
Member

Choose a reason for hiding this comment

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

good catch!

@ngoldbaum ngoldbaum added component: numpy.strings String dtypes and functions 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes labels May 26, 2025
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label May 26, 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.

Ah, hand't thought of this, thought we should just add the check. But this seems safe to me.

No big opinion on the error, but agree your suggested edit is a bit nicer.

@seberg
Copy link
Member

seberg commented May 26, 2025

Hmmm, sorry hadn't quite thought that through. This works to prevent in-place ops, it will not fix np.multiple(str_arr, 10, out=str_arr.copy()).
Maybe that is good enough for now at least, although I suspect that checking the length inside the ufunc isn't a big deal (but maybe a bit tedious).

@ngoldbaum
Copy link
Member Author

I suspect that checking the length inside the ufunc isn't a big deal (but maybe a bit tedious).

Yeah this is probably the way to go. We can get the value of the right hand side, compare it with the dtype's width, and use that bound to prevent any of the loops from overflowing the buffer.

That way I can also avoid messing with the types or breaking backward compatibility, even if in-place multiplying a string is a weird thing to do...

I'm going to re-issue this with a different approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy.strings String dtypes and functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: in-place fixed-width string multiply doesn't do overflow checking
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy