Skip to content

Commit 007f60c

Browse files
committed
fix(chunk-grids): accept sequences that only define __getitem__ and __len__
The chunk normalizer checked `isinstance(chunks, Iterable)` before calling `list(chunks)`. `Iterable` does not recognize a sequence that implements only `__getitem__` and `__len__`, which `list` accepts and which zarr 3.4.0 took as a chunk specification. The normalizer now calls `list` and turns its `TypeError` into the chunk specification error. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 86fca10 commit 007f60c

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎src/zarr/core/chunk_grids.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import math
66
import operator
77
import warnings
8-
from collections.abc import Iterable
98
from dataclasses import dataclass, field
109
from functools import reduce
1110
from typing import (
@@ -32,7 +31,7 @@
3231
from zarr.errors import ZarrUserWarning
3332

3433
if TYPE_CHECKING:
35-
from collections.abc import Iterator, Sequence
34+
from collections.abc import Iterable, Iterator, Sequence
3635

3736
from zarr.core.array import ShardsLike
3837
from zarr.core.common import ChunksLike
@@ -354,15 +353,16 @@ def _chunk_int(value: object) -> int | None:
354353
def _chunk_list(chunks: object) -> list[Any]:
355354
"""Return the elements of a chunk specification that is not an integer, or raise
356355
a `TypeError` naming it (a float, `None`, a 0-d non-integer array)."""
357-
if isinstance(chunks, Iterable):
358-
try:
359-
return list(chunks)
360-
except TypeError:
361-
pass # a 0-d numpy array is `Iterable` by type, but iterating it raises
362-
raise TypeError(
363-
f"Chunk specification must be an integer or an iterable of integers; got "
364-
f"{chunks!r} of type {type(chunks).__name__}."
365-
)
356+
try:
357+
# `list` also takes a sequence with only `__getitem__` and `__len__`, which
358+
# `Iterable` does not recognize.
359+
return list(cast("Iterable[Any]", chunks))
360+
except TypeError:
361+
# Not iterable, or a 0-d numpy array, whose iteration raises.
362+
raise TypeError(
363+
f"Chunk specification must be an integer or an iterable of integers; got "
364+
f"{chunks!r} of type {type(chunks).__name__}."
365+
) from None
366366

367367

368368
def is_regular_1d(dim_chunks: Sequence[int]) -> bool:

‎tests/test_chunk_grids.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
6161
assert all(0 < c <= max(s, 1) for c, s in zip(chunks, shape, strict=False))
6262

6363

64+
class _IndexedSequence:
65+
"""A sequence with only `__getitem__` and `__len__`: `list` takes it, but
66+
`collections.abc.Iterable` does not recognize it."""
67+
68+
def __init__(self, *items: int) -> None:
69+
self._items = items
70+
71+
def __getitem__(self, index: int) -> int:
72+
return self._items[index]
73+
74+
def __len__(self) -> int:
75+
return len(self._items)
76+
77+
6478
@pytest.mark.parametrize(
6579
("chunks", "shape", "expected"),
6680
[
@@ -110,6 +124,9 @@ def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
110124
((np.array([60, 40]), np.array([50, 50])), (100, 100), ((60, 40), (50, 50))),
111125
# a Python `bool` inside a specification is an `int`: `True` is a chunk size of 1
112126
((True, 5), (10, 10), (1, 5)),
127+
# a sequence with only `__getitem__` and `__len__` is a list of chunk sizes
128+
(_IndexedSequence(5, 5), (10, 10), (5, 5)),
129+
((_IndexedSequence(6, 4), 5), (10, 10), ((6, 4), 5)),
113130
([[True, 9], 10], (10, 10), ((1, 9), 10)),
114131
],
115132
)

0 commit comments

Comments
 (0)