Skip to content

fix: dispatch short boolean ndarray masks like NumPy - #4519

Open
XXXXXQ-0206 wants to merge 2 commits into
zarr-developers:mainfrom
XXXXXQ-0206:fix/bool-array-mask-dispatch
Open

XXXXXQ-0206 wants to merge 2 commits into
zarr-developers:mainfrom
XXXXXQ-0206:fix/bool-array-mask-dispatch

Conversation

@XXXXXQ-0206

Copy link
Copy Markdown

Summary

A boolean ndarray used as a bare mask raises, while the equivalent list form and .oindex work:

a = zarr.create_array({}, data=np.arange(16).reshape(4, 4), chunks=(2, 2))
a[[True, False, True, False]]                    # (2, 4)
a[np.array([True, False, True, False])]          # VindexInvalidSelectionError
a.oindex[np.array([True, False, True, False])]   # (2, 4)

An empty boolean array (a[np.array([], dtype=bool)]) fails the same way, while NumPy returns (0, 4).

Cause: is_pure_fancy_indexing (src/zarr/core/indexing.py:283-285) returns True for any boolean ndarray, so Array.__getitem__ (src/zarr/core/array.py:2750-2751) takes the vindex path, and VIndex.__getitem__ (indexing.py:1441-1462) only accepts coordinate arrays or a full-shape mask. The orthogonal path already handles short masks today (a.oindex[mask] gives (2, 4)), so this is a dispatch problem rather than a missing capability.

The change is in two places: only full-shape masks take the vectorized path (selection.ndim == ndim and selection.size > 0), and BoolArrayDimIndexer accepts a zero-length mask. A non-empty mask whose length does not match the axis still raises, as NumPy does.

For reviewers

Please confirm that keeping the length-mismatch error is the behaviour you want — it matches NumPy, and the new tests pin it as a negative case.

Testing

The new tests are parameterised over zarr_format [2, 3]. They fail on the base revision (src blob 8ea59eb9 -> 17970b6a) and pass with the change. Full suite, pytest -q --ignore=tests/benchmarks: 12414 passed, 1225 skipped, 4 xfailed. ruff check / ruff format --check are clean. Checked against NumPy 2.5.4 and 2.0.2 (the version pinned in min_deps).

Not covered here: 0-d boolean arrays and multi-dimensional partial masks (for example a 3-D array with a (2, 3) mask) raise both before and after this change. That is not a regression, but it is a separate discussion.

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 17, 2026 02:15

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 17, 2026
XXXXXQ-0206 added a commit to XXXXXQ-0206/zarr-python that referenced this pull request Oct 17, 2026
@github-actions github-actions Bot removed the needs release notes Automatically applied to PRs which haven't added release notes label Oct 17, 2026
@codecov

codecov Bot commented Oct 17, 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 (7a3ec17).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4519   +/-   ##
=======================================
  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/bool-array-mask-dispatch branch from 1b79da9 to 7a3ec17 Compare October 17, 2026 09:26

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