Skip to content

BUG: fix two error handling mistakes in stringdtype replace loop - #32355

Merged
charris merged 1 commit into
numpy:mainfrom
ngoldbaum:fix-error-handling-replace
Aug 20, 2026
Merged

charris merged 1 commit into
numpy:mainfrom
ngoldbaum:fix-error-handling-replace

Conversation

@ngoldbaum

Copy link
Copy Markdown
Member

PR summary

Two error paths in the StringDType replace implementation had bugs. One one is user-visible via a public API:

In [1]: arr = np.array(["aaa"], dtype="T")

In [2]: np.strings.replace(arr, "X", count=-12)
Out[2]: array(['aaa'], dtype=StringDType())

This should be array(['XXX'], dtype=StringDType()), for any negative count. Right now it only happens if count == -1. This follows python strings:

In [3]: "aaa".replace('a', 'X', count=-12)
Out[3]: 'XXX'

The other one requires passing out to the private _replace ufunc directly to have a user-visible impact. Still, seems worth adding the goto fail that's missing.

AI Disclosure

An AI model spotted both issues.

@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.strings String dtypes and functions labels Aug 19, 2026
@charris
charris merged commit 97ba17b into numpy:main Aug 20, 2026
102 of 104 checks passed
@charris

charris commented Aug 20, 2026

Copy link
Copy Markdown
Member

Thanks Nathan.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Aug 21, 2026
charris added a commit that referenced this pull request Aug 21, 2026
BUG: fix two error handling mistakes in stringdtype replace loop (#32355)
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 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.

2 participants