Skip to content

TYP: fix random.Generator.choice output type for int sequences - #31877

Merged
charris merged 1 commit into
numpy:mainfrom
jorenham:typing/random.Generator.choice-sctype
Jul 6, 2026
Merged

charris merged 1 commit into
numpy:mainfrom
jorenham:typing/random.Generator.choice-sctype

Conversation

@jorenham

@jorenham jorenham commented Jul 6, 2026 •

Copy link
Copy Markdown
Member

Both these cases were typed as int in the stubs:

In [1]: rng.choice(3)
Out[1]: 0

In [2]: rng.choice([1,2,3])
Out[2]: np.int64(1)

The second (rng.choice([1,2,3])) case was wrong, and is now correctly annotated to return np.int_ in the stubs as well.

This matters because int and np.int_ are incompatible types.


Fully written by me (a human, I think); bug found by Claude Fable 5 (high) (here's the full audit for those interested).

@rkern

rkern commented Jul 6, 2026

Copy link
Copy Markdown
Member

If you just omit that overload, does it not get caught by the _ArrayLike[ScalarT] that follows? Or am I expecting too much out of _ArrayLike[ScalarT]?

@jorenham

jorenham commented Jul 6, 2026 •

Copy link
Copy Markdown
Member Author

If you just omit that overload, does it not get caught by the _ArrayLike[ScalarT] that follows? Or am I expecting too much out of _ArrayLike[ScalarT]?

_ArrayLike[ScalarT] only matches array-likes with a known numpy scalar type (ScalarT: np.generic), so it indeed won't match a sequence of builtin python ints.

@charris
charris merged commit d376aa2 into numpy:main Jul 6, 2026
14 checks passed
@charris

charris commented Jul 6, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

@jorenham
jorenham deleted the typing/random.Generator.choice-sctype branch July 6, 2026 17:07
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 6, 2026
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants