Skip to content

feat(numpy): support negative indices in checked array accessors - #6201

Merged
rwgk merged 5 commits into
pybind:masterfrom
rwgk:numpy-normalize-negative-indices
Oct 24, 2026
Merged

rwgk merged 5 commits into
pybind:masterfrom
rwgk:numpy-normalize-negative-indices

Conversation

@rwgk

@rwgk rwgk commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Alternative to #6187, retaining both of its commits and adding NumPy-style negative indexing for checked array/array_t accessors and negative axes for shape()/strides().

#6187 rejects all negative indices; this wraps valid negative indices (-1 selects the last element or axis) and raises IndexError for indices outside the valid range. Unchecked accessors keep their existing behavior. See the discussion and local cost comparison.

Validation: local default and free-threaded CPython 3.14.4 test suites and prek -a --quiet passed.

Suggested changelog entry:

  • Support NumPy-style negative indices and axes in checked array and array_t accessors.

📚 Documentation preview 📚: https://pybind11--6201.org.readthedocs.build/

sayed0200 and others added 3 commits October 7, 2026 11:43
Use a single unsigned bounds compare, say in the error message that
negative indices are not supported, document the behavior on the
accessors, and cover offset_at/offset_at_t in test_bounds_check.

Assisted-by: ClaudeCode:claude-opus-5-5
rwgk and others added 2 commits October 23, 2026 19:23
I accidentally merged pybind#6187 instead of this PR. I’ve now merged `master` into this branch, preserving exactly the same Git tree as the previously tested version of pybind#6201. Only the commit history has changed, so I’ll merge without running CI again.
@rwgk

rwgk commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator Author

I accidentally merged #6187 instead of this PR. I’ve now merged master into this branch, preserving exactly the same Git tree as the previously tested version of #6201. Only the commit history has changed, so I’ll merge without running CI again.

@rwgk
rwgk merged commit 11ee91e into pybind:master Oct 24, 2026
1 of 3 checks passed
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Oct 24, 2026
@rwgk
rwgk deleted the numpy-normalize-negative-indices branch October 24, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants