Skip to content

BUG: don't assume strides are a multiple of itemsize in stringdtype casts - #32273

Merged
ngoldbaum merged 2 commits into
numpy:mainfrom
ngoldbaum:fix-cast-strides-division
Aug 13, 2026
Merged

BUG: don't assume strides are a multiple of itemsize in stringdtype casts#32273
ngoldbaum merged 2 commits into
numpy:mainfrom
ngoldbaum:fix-cast-strides-division

Conversation

@ngoldbaum

Copy link
Copy Markdown
Member

PR summary

See the test I added. On NumPy 2.5.2 you can get a cast to generate garbage by casting from void to StringDType:

>>> rec = np.zeros(3, dtype=[("c", "c16"), ("f", "f8")])
... rec["c"] = [1 + 1j, 2 + 2j, 3 + 3j]
... rec["f"] = [10.0, 20.0, 30.0]
...
>>> rec["c"].astype("T")
array(['(1+1j)', '(10+2j)', '(2+20j)'], dtype=StringDType())
>>> rec["c"]
array([1.+1.j, 2.+2.j, 3.+3.j])
>>> np.__version__
'2.5.2'

AI Disclosure

An AI model spotted this bug and helped me with the test case.

@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Aug 12, 2026
@MaanasArora

Copy link
Copy Markdown
Contributor

Thanks, should we not test for datetime/timedelta as well? Otherwise the fix looks good to me! (unicode doesn't seem to be impacted, so makes sense to skip it.)

@ngoldbaum

Copy link
Copy Markdown
Member Author

Thanks for giving this a look @MaanasArora. I added the tests you asked for and I expect them to be non-controversial so I'll just hit the merge button.

@ngoldbaum
ngoldbaum merged commit dd18992 into numpy:main Aug 13, 2026
91 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Aug 14, 2026
charris added a commit that referenced this pull request Aug 14, 2026
BUG: don't assume strides are a multiple of itemsize in stringdtype casts (#32273)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants