Repository navigation
fix(indexing): treat an empty list as an empty integer selection - #4518
XXXXXQ-0206 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
202518f to
d018fdd
Compare
|
|
||
|
|
||
| def replace_lists(selection: SelectionNormalized) -> SelectionNormalized: | ||
| def _as_array(dim_sel: list[Any]) -> Any: |
There was a problem hiding this comment.
don't nest function definitions without a reason
| @@ -510,9 +510,16 @@ def replace_ellipsis(selection: Any, shape: tuple[int, ...]) -> SelectionNormali | |||
|
|
|||
|
|
|||
| def replace_lists(selection: SelectionNormalized) -> SelectionNormalized: | |||
There was a problem hiding this comment.
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
)|
Addressed both review points in |
Summary
arr[[]]raisesIndexError: unsupported selection item for basic indexing. The same selection works as an empty integer array, and NumPy accepts it:IntSequence(declared atsrc/zarr/core/indexing.py:42) includeslist[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) andis_bool_list(:238) both requirelen(x) > 0, so an empty list falls through toBasicIndexer, which accepts only int and slice.The change drops
len(x) > 0fromis_integer_listonly, and makesreplace_listsusenp.intpfor an empty list (it otherwise infers float64).is_bool_listkeeps 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,
oindexandvindexforms, plus the empty-array, boolean-list and write controls. It fails on the base revision (src blob8ea59eb9->2926551a);tests/test_indexing.pygoes from 592 to 594 passed.ruff check/ruff format --checkare 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_listuntouched is how you want[]disambiguated.Author attestation
TODO
docs/user-guide/*.mdchanges/