Skip to content

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

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

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

Conversation

@XXXXXQ-0206

@XXXXXQ-0206 XXXXXQ-0206 commented Oct 16, 2026 •

Copy link
Copy Markdown

Summary

arr[[]] raises IndexError: unsupported selection item for basic indexing. The same selection works as an empty integer array, 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)

IntSequence (declared at src/zarr/core/indexing.py:42) includes list[int], so an empty list is a valid selection. The empty array form already returns (0, 4); only the list form is rejected.

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 stays an empty integer selection rather than 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 goes from 592 to 594 passed. ruff check / ruff format --check are clean.

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

For reviewers

The boolean-list guard is deliberately unchanged — please check that leaving is_bool_list untouched is how you want [] disambiguated.

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)

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
XXXXXQ-0206 added a commit to XXXXXQ-0206/zarr-python that referenced this pull request 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 (ec2c359) to head (8b3d44e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4518   +/-   ##
=======================================
  Coverage   94.71%   94.71%           
=======================================
  Files          94       94           
  Lines       13663    13663           
=======================================
  Hits        12941    12941           
  Misses        722      722           
Files with missing lines Coverage Δ
src/zarr/core/indexing.py 96.57% <100.00%> (ø)
🚀 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.

@XXXXXQ-0206
XXXXXQ-0206 force-pushed the fix/empty-list-selection branch from 202518f to d018fdd Compare October 17, 2026 09:27
Comment thread src/zarr/core/indexing.py Outdated


def replace_lists(selection: SelectionNormalized) -> SelectionNormalized:
def _as_array(dim_sel: list[Any]) -> Any:

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.

don't nest function definitions without a reason

Comment thread src/zarr/core/indexing.py
@@ -510,9 +510,16 @@ def replace_ellipsis(selection: Any, shape: tuple[int, ...]) -> SelectionNormali


def replace_lists(selection: SelectionNormalized) -> SelectionNormalized:

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.

consider this instead:

def replace_lists(selection: SelectionNormalized) -> SelectionNormalized:
    # An empty list has no dtype to infer; NumPy indexes with it as an empty integer array.
    return tuple(
        cast("ArrayOfIntOrBool", np.asarray(dim_sel, dtype=np.intp if len(dim_sel) == 0 else None))
        if isinstance(dim_sel, list)
        else dim_sel
        for dim_sel in selection
    )

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