Skip to content

fix: unsigned index arrays raise IndexError because np.diff wraps and uint64 promotes - #4286

Open
selmanozleyen wants to merge 4 commits into
zarr-developers:mainfrom
selmanozleyen:fix/index-overflow-cases
Open

fix: unsigned index arrays raise IndexError because np.diff wraps and uint64 promotes#4286
selmanozleyen wants to merge 4 commits into
zarr-developers:mainfrom
selmanozleyen:fix/index-overflow-cases

Conversation

@selmanozleyen

@selmanozleyen selmanozleyen commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Hi,

fixes: #4285

Summary

An integer array selection with an unsigned dtype could fail where the same values in a
signed dtype worked. is_integer_array() accepts dtype.kind in "ui", so these selections
pass validation and are then mishandled — the dtype was never normalized to intp, which
IntArrayDimIndexer already claims in its annotation.

  • Order.check used np.diff, which wraps on unsigned dtypes: np.diff(uint8([3, 0])) is
    253, not -3. A descending selection was classified INCREASING, so the argsort
    grouping indices by chunk was skipped and the lookup hit the wrong chunk.
  • uint64 promotes to float64 against the signed chunk offset, on [] and .vindex
    alike, sorted or not.

Both raised rather than returning wrong data. Fixed by comparing instead of subtracting in
Order.check, and casting to intp in IntArrayDimIndexer and CoordinateIndexer.

For reviewers

The CoordinateIndexer cast sits after is_coordinate_selection on purpose — above it, a
float coordinate array would be silently truncated instead of raising. No test pins that.

Selections that previously raised now succeed; nothing that previously worked changes.

Author attestation

  • I am a human, these are my changes, and I have reviewed and understood every change and can explain why each is correct.

TODO

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.md
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

@github-actions github-actions Bot added the needs release notes Automatically applied to PRs which haven't added release notes label Aug 25, 2026
@github-actions github-actions Bot removed the needs release notes Automatically applied to PRs which haven't added release notes label Aug 25, 2026
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.19%. Comparing base (d34f58a) to head (1b6010a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4286   +/-   ##
=======================================
  Coverage   94.19%   94.19%           
=======================================
  Files          92       92           
  Lines       12825    12826    +1     
=======================================
+ Hits        12080    12081    +1     
  Misses        745      745           
Files with missing lines Coverage Δ
src/zarr/core/indexing.py 96.42% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@selmanozleyen
selmanozleyen marked this pull request as ready for review August 25, 2026 20:56
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.

Unsigned integer index arrays are mishandled (IndexError on unsorted, and on any uint64)

1 participant