Skip to content

Deprecate scalar upcast in dpnp.append with axis=None - #3125

Open
antonwolfy wants to merge 1 commit into
masterfrom
deprecate-append-scalar-promotion
Open

antonwolfy wants to merge 1 commit into
masterfrom
deprecate-append-scalar-promotion

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

`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 antonwolfy added this to the 0.22.0 release milestone Oct 25, 2026
@antonwolfy antonwolfy self-assigned this Oct 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3125/index.html

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 82.642% (+0.02%) from 82.627% — deprecate-append-scalar-promotion into master

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants