ENH: ``numpy.where``: don't silently truncate Python scalars by SpecLad · Pull Request #30803 · numpy/numpy · GitHub
Skip to content

ENH: numpy.where: don't silently truncate Python scalars - #30803

Merged
seberg merged 1 commit into
numpy:mainfrom
SpecLad:where-truncation
Mar 6, 2026
Merged

seberg merged 1 commit into
numpy:mainfrom
SpecLad:where-truncation

Conversation

@SpecLad

@SpecLad SpecLad commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Since NumPy 2.0, code like the following will cause a scalar value to be truncated:

>>> np.where([False], np.array([1], dtype=np.uint8), 1000)
array([232], dtype=uint8)

(in NumPy 1.x, it would instead output a uint16 array)

This is dangerous, and inconsistent with other NumPy functions, e.g.:

>>> np.multiply(np.array([1], dtype=np.uint8), 1000)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    np.multiply(np.array([1], dtype=np.uint8), 1000)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: Python integer 1000 out of bounds for uint8

Use npy_update_operand_for_scalar to bring the behavior of where in line with other functions.

Closes #27006.

Since NumPy 2.0, code like the following will cause a scalar value to be
truncated:

    >>> np.where([False], np.array([1], dtype=np.uint8), 1000)
    array([232], dtype=uint8)

(in NumPy 1.x, it would instead output a uint16 array)

This is dangerous, and inconsistent with other NumPy functions, e.g.:

    >>> np.multiply(np.array([1], dtype=np.uint8), 1000)
    Traceback (most recent call last):
      File "<python-input-3>", line 1, in <module>
        np.multiply(np.array([1], dtype=np.uint8), 1000)
        ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    OverflowError: Python integer 1000 out of bounds for uint8

Use `npy_update_operand_for_scalar` to bring the behavior of `where` in line
with other functions.

Closes #27006.
@SpecLad
SpecLad marked this pull request as ready for review February 8, 2026 21:11
@ngoldbaum

Copy link
Copy Markdown
Member

@jorenham jorenham changed the title ENH: numpy.where: don't silently truncate Python scalars ENH: numpy.where: don't silently truncate Python scalars Feb 20, 2026

@seberg seberg 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.

Thanks, sorry for not looking at it closer earlier. I hadn't realized that we already fixed the promotion part, but just the error was missing.

LGTM (Staring at this now, I am wondering if we may want to push this into the iterator even, but not sure that is good and it isn't relevant here).

@seberg
seberg merged commit 2430050 into numpy:main Mar 6, 2026
77 checks passed
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.

where with out-of-bounds Python integers in Numpy 2

3 participants