Skip to content

BUG: fix string outputs of vectorize with signature - #33541

Open
danggeun wants to merge 4 commits into
numpy:mainfrom
danggeun:bugfix/lib-vectorize-signature-str
Open

danggeun wants to merge 4 commits into
numpy:mainfrom
danggeun:bugfix/lib-vectorize-signature-str

Conversation

@danggeun

@danggeun danggeun commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

>>> f = np.vectorize(lambda n: "x" * n, signature="()->()")
>>> f([1, 3, 5])
array(['x', 'x', 'x'], dtype='<U1')
>>> np.vectorize(lambda n: "x" * n)([1, 3, 5])
array(['x', 'xxx', 'xxxxx'], dtype='<U5')

With a signature, vectorize creates the output arrays from the first result, so longer strings that come later are silently truncated. With a string type in otypes, the length is dropped when otypes is stored, so the array is created as <U1 / S1 and every result is cut to one character.

String outputs are now collected in object arrays and converted at the end, when the longest result is known, as the path without a signature already does.

Outputs that are not strings are unchanged. A length given in otypes is still not kept, as without a signature: otypes=["<U10"] gives <U2 when the results are '64' and '7'.

Testing: test_function_base.py gives 1438 passed. 11 of the 13 new tests fail without the fix; the other two check that behaviour from main is kept.

Closes #23442.

AI Disclosure

I used an AI assistant (Claude) to prototype the patch and the tests, and to put this description into English from my notes. I ran test_function_base.py before and after the patch and a behaviour diff over string and non-string outputs myself.

Comment thread numpy/lib/_function_base_impl.py Resolved
@danggeun
danggeun requested a review from yimingdx December 6, 2026 01:14

@InessaPawson InessaPawson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for working on this, @danggeun!

A couple of things to fix:

  1. With a signature, 0-dimensional byte-string array results come out too wide. Could you fix it and add a regression test?
  2. Could you also add a test for non-scalar string outputs?

@seberg, do you think the changes in behavior for chararray and size-0 string arrays are acceptable here?

@InessaPawson

Copy link
Copy Markdown
Member

@mhvk, since you introduced subclass preservation for vectorize with a signature in #19356, I'd be interested in your thoughts on the chararray behavior change here as well (it now raises ValueError).

@mhvk

mhvk commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Hmm, I'm not sure about this. For general gufuncs working on strings, one has to give an explicit dtype as well, so it is not clear why for np.vectorize one should work around it. Instead of silently casting inside, perhaps we should just document the limitation and suggest that one pass in otypes=["O"] for cases like variable-length strings? Actually, for that case specifically, one can do better and use otypes=["T"] (the newer variable length StringDType, which passes the above). One can always cast to U after.

Note that what I do find a bug is that I cannot pass in otypes=["U5"] or so.

p.s. On subclasses, I did not check why they would not work in the present PR, but think that they should.

@danggeun

danggeun commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor Author
  1. Done: 0-d array results are now stored as scalars. Added a test.
  2. Added.

I looked again at the two behaviour changes listed in the description, and both can be avoided. I updated the description.

Size-0 results now keep their dtype, as on main.

In a separate commit, chararray results are viewed as plain arrays and turned back into a chararray at the end, so a chararray is returned as before. If you'd rather not handle chararray this way, I'll drop that commit.

@mhvk, without a signature, vectorize already collects the results in an object array and casts them at the end (np.vectorize(lambda n: "x" * n)([1, 3]) gives <U3); this PR uses the same approach with a signature. otypes=["T"] works, as you say, but on main a call with a signature and no otypes is silently truncated. The width in otypes=["U5"] is dropped with or without a signature, and keeping it would truncate results that now come out whole, so I think it is outside the scope of this PR.

@danggeun
danggeun requested a review from InessaPawson December 6, 2026 18:55

This branch has not been deployed

No deployments
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.

BUG: vectorize truncates string outputs to 1 character, even with explicitly-specified otypes

4 participants