Skip to content

BUG: centralized helper for output coerce and na_object in stringdtype operations - #31884

Merged
charris merged 1 commit into
numpy:mainfrom
ngoldbaum:nan-coerce-consistency
Jul 8, 2026
Merged

charris merged 1 commit into
numpy:mainfrom
ngoldbaum:nan-coerce-consistency

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Jul 6, 2026 •

Copy link
Copy Markdown
Member

PR summary

Related to #31825 but issuing it as a separate PR because the two changes don't overlap at all.

We currently have custom logic for determining the correct na_object and coerce value when operations mix StringDType instances. In some cases this can even lead to crashes. The following script segfaults for me because the na_object in the fill character array is not being handled correctly.

Python 3.14.5 (main, May 22 2026, 08:10:06) [Clang 21.0.0 (clang-2100.0.123.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from numpy.dtypes import StringDType
>>> print(np.__version__)
2.5.1
>>> arr = np.array(["a"], dtype="T")
>>> fill = np.array("*", dtype=StringDType(na_object="*"))
>>> print(np.strings.center(arr, 5, fill))
[1]    87011 segmentation fault  python

This centralizes the logic for handling the na_object and coerce argument in ufunc loops and in the comparison machinery into one set of shared helpers.

Also adds tests for this behavior, including the case that leads to a crash above.

First time committer introduction

AI Disclosure

I found this issue using an AI model. I also used an AI model to help with the tests and the approach for the fix.

@ngoldbaum ngoldbaum added this to the 2.5.2 release milestone Jul 6, 2026
@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.strings String dtypes and functions labels Jul 6, 2026
@ngoldbaum
ngoldbaum force-pushed the nan-coerce-consistency branch from 0ce76e4 to 2f6a487 Compare July 6, 2026 19:57
@ngoldbaum

Copy link
Copy Markdown
Member Author

@mhvk hope you don't mind a ping to look this over. #31825 as well if you have the bandwidth 🙈.

@ngoldbaum ngoldbaum changed the title BUG: centralize handling of operand order in stringdtype operations BUG: centralized helper for output coerce and na_object in stringdtype operations Jul 6, 2026
@ngoldbaum

Copy link
Copy Markdown
Member Author

@Ethan0456 you just put in a PR for a similar issue. Maybe you want to take a look at this?

@Ethan0456

Copy link
Copy Markdown
Contributor

@ngoldbaum LGTM! thanks for the clear repro. I reproduced the center segfault on 2.5.1; fixed on this branch with ['**a**']. The shared helpers match the intended rules, and scoping resolve to nin avoids walking NULL outs on ops like center. New mixed-instance tests look right and passed locally.

@charris
charris merged commit e4b464d into numpy:main Jul 8, 2026
87 checks passed
@charris

charris commented Jul 8, 2026

Copy link
Copy Markdown
Member

Thanks Nathan.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 10, 2026
charris pushed a commit that referenced this pull request Jul 10, 2026
charris added a commit that referenced this pull request Jul 10, 2026
BUG: centralized helper for output coerce and na_object in stringdtype operations (#31884)
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Aug 4, 2026
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug component: numpy.strings String dtypes and functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants