Skip to content

fix(indexing): treat an empty list as an empty integer selection - #4518

Open
XXXXXQ-0206 wants to merge 2 commits into
zarr-developers:mainfrom
XXXXXQ-0206:fix/empty-list-selection
Open

XXXXXQ-0206 wants to merge 2 commits into
zarr-developers:mainfrom
XXXXXQ-0206:fix/empty-list-selection

Conversation

@XXXXXQ-0206

Copy link
Copy Markdown

arr[[]] raises IndexError: unsupported selection item for basic indexing, while the same selection as an empty integer array works and NumPy accepts it:

a = zarr.create_array({}, data=np.arange(16).reshape(4, 4), chunks=(2, 2))
a[[]]                            # IndexError
a[np.array([], dtype=np.intp)]   # (0, 4)
np.arange(16).reshape(4, 4)[[]]  # (0, 4)

Why it is wrong:

  1. IntSequence (declared at src/zarr/core/indexing.py:42) includes list[int], so an empty list is a valid selection.
  2. The empty array form already works inside the library (is_integer_array ignores length) and returns (0, 4); only the list form is rejected.
  3. NumPy treats [] as an empty integer selection.

Cause: is_integer_list (:233) and is_bool_list (:238) both require len(x) > 0, so an empty list falls through to BasicIndexer, which accepts only int and slice.

The change drops len(x) > 0 from is_integer_list only and makes replace_lists use np.intp for an empty list (it otherwise infers float64). is_bool_list keeps its guard, so an empty list is an empty integer selection, not an empty mask, as in NumPy.

The new test covers empty lists in bare, tuple, oindex and vindex forms, plus the empty-array, boolean-list and write controls. It fails on the base revision (src blob 8ea59eb9 -> 2926551a); tests/test_indexing.py 592 -> 594 passed. Repo-wide pytest: 12527 passed, 1293 skipped, 4 xfailed (Windows, CPython 3.14.7, 24m); ruff check/format --check clean.

If you would rather keep an empty list raising, we are happy to adjust.

AI-assisted, prepared under the account owner's direction.

Copilot AI balanced review requested due to automatic review settings October 16, 2026 18:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.71%. Comparing base (bd0dc84) to head (202518f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4518   +/-   ##
=======================================
  Coverage   94.71%   94.71%           
=======================================
  Files          94       94           
  Lines       13663    13665    +2     
=======================================
+ Hits        12941    12943    +2     
  Misses        722      722           
Files with missing lines Coverage Δ
src/zarr/core/indexing.py 96.58% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants