Skip to content

Fix dpnp.insert ignoring out-of-bounds negative indices in multi-element obj - #3041

Open
antonwolfy wants to merge 3 commits into
masterfrom
fix-insert-oob-negative-indices
Open

Fix dpnp.insert ignoring out-of-bounds negative indices in multi-element obj#3041
antonwolfy wants to merge 3 commits into
masterfrom
fix-insert-oob-negative-indices

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

dpnp.insert has two index paths: the singleton path (_insert_singleton_index) already validated bounds, but the multi-element array path (_insert_array_indices) normalized negative indices with indices[indices < 0] += n without any bounds check. As a result, an out-of-bounds negative index mixed with in-bounds ones — e.g. dpnp.insert([0, 1, 2], [-6, 0], [9, 8]) — silently produced a wrong result instead of raising.

This PR adds the same bounds validation NumPy introduced: if any index is < -n or > n, IndexError is raised with the standard index {i} is out of bounds for axis {axis} with size {n} message. In-bounds and out-of-bounds indices mixed in a single call now consistently raise.

  • 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?

@antonwolfy antonwolfy self-assigned this Aug 25, 2026
@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 25, 2026
@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev6=py314ha0e2e8e_20 ran successfully.
Passed: 1375
Failed: 0
Skipped: 7

@coveralls

coveralls commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.484% (+0.001%) from 78.483% — fix-insert-oob-negative-indices into master

@github-actions

Copy link
Copy Markdown
Contributor

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

@antonwolfy
antonwolfy marked this pull request as ready for review August 26, 2026 11:16
…ent obj

Backport of numpy#31782. The multi-element index path in
_insert_array_indices normalized negative indices without any bounds
check, so an out-of-bounds negative index mixed with in-bounds ones
silently produced wrong results instead of raising. Add the same
bounds validation numpy uses so any out-of-bounds index raises
IndexError, and extend the test to cover the mixed case.
Parametrize the out-of-bounds test over both numpy and dpnp and add
multi-element cases that exercise the newly added bounds check in
_insert_array_indices, including the positive (max > n) branch, plus an
ND case validating axis/size reporting for a non-zero axis.
@antonwolfy
antonwolfy force-pushed the fix-insert-oob-negative-indices branch from 68c84e6 to 97b4495 Compare August 26, 2026 17:57
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.

2 participants