Repository navigation
Deprecate scalar upcast in dpnp.append with axis=None - #3125
Merged
Merged
Conversation
`dpnp.append` converts a Python numeric scalar in `values` to an array before choosing the result dtype, so it can upcast `arr` (e.g. `int8` array and `1` give `int64`). Emit a `FutureWarning` when Python scalar promotion rules would give a different dtype, aligning with NumPy 2.6. The result is unchanged. Python bool, NumPy scalars and lists do not warn.
antonwolfy
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
October 25, 2026 12:37
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
Collaborator
ndgrigorian
approved these changes
Oct 26, 2026
Contributor
|
Array API standard conformance tests for dpnp=0.21.0dev16=np2py314h8d9cdd5_13 ran successfully. |
github-actions Bot
added a commit
that referenced
this pull request
Oct 28, 2026
`dpnp.append` with `axis=None` converts a Python numeric scalar in `values` to an array before choosing the result dtype, so the scalar can upcast `arr`. NumPy 2.6 deprecates this behavior: a future release will use the Python scalar promotion rules instead, as `concatenate` does. This PR mirrors that deprecation. ```python a = dpnp.ones(3, dtype=dpnp.int8) dpnp.append(a, 1).dtype # int64, now emits FutureWarning (int8 in the future) dpnp.append(a, [1]).dtype # int64, no warning dpnp.append(a, dpnp.asarray(1, dtype=a.dtype, device=a.device)).dtype # int8, no warning ``` The result is unchanged, but the call now emits: ``` dpnp.append converted Python numeric scalars to arrays before choosing the result dtype, producing dtype int64. This behavior will change in a future release, these inputs will use dtype int8, which may round values differently or raise an error. Wrap the scalar in a list to keep the current result, or convert it to an array of dtype 'int8' for the future behavior. ``` The warning is emitted only when the dtype will actually change, i.e. when `dpnp.result_type(arr, values)` differs from the current result dtype. As in NumPy, only exact Python `int`, `float` and `complex` scalars are affected; Python `bool`, NumPy scalars, lists and arrays don't warn. Values out of range for the future dtype (e.g. `300` with a `uint8` array) will raise `OverflowError` once the change lands. On devices without fp64 support, a Python `float` or `complex` scalar already converts to a single-precision array, so e.g. a `float32` array keeps its dtype and no warning is emitted there. bc42b29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dpnp.appendwithaxis=Noneconverts a Python numeric scalar invaluesto an array before choosing the result dtype, so the scalar can upcastarr. NumPy 2.6 deprecates this behavior: a future release will use the Python scalar promotion rules instead, asconcatenatedoes. This PR mirrors that deprecation.The result is unchanged, but the call now emits:
The warning is emitted only when the dtype will actually change, i.e. when
dpnp.result_type(arr, values)differs from the current result dtype. As in NumPy, only exact Pythonint,floatandcomplexscalars are affected; Pythonbool, NumPy scalars, lists and arrays don't warn. Values out of range for the future dtype (e.g.300with auint8array) will raiseOverflowErroronce the change lands.On devices without fp64 support, a Python
floatorcomplexscalar already converts to a single-precision array, so e.g. afloat32array keeps its dtype and no warning is emitted there.