Skip to content

Commit bc306aa

Browse files
d-v-bclaude
andcommitted
fix(metadata): leave a sharded chunk size of 0 unread when the inner shape is invalid
A stored outer chunk size of 0 of a sharded array is read in multiples of the inner chunk size. When that inner size is itself 0 or `false`, the unit is unknown: the upgrade now leaves the 0 for the constructor, which rejects it with the same `ValueError` zarr 3.4.0 raised, instead of a ZeroDivisionError. `_read_codec` now matches the sharding codec like `_inner_chunk_shape` does. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f78e115 commit bc306aa

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

‎src/zarr/core/metadata/upgrades.py‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import warnings
2323
from collections.abc import Callable, Iterable, Mapping, Sequence
2424
from itertools import chain, repeat
25-
from typing import TYPE_CHECKING, Final, TypeGuard
25+
from typing import TYPE_CHECKING, Final, TypeGuard, cast
2626

2727
from zarr.core._json import json_equal
2828
from zarr.core.chunk_grids import full_span_chunk_size
@@ -137,20 +137,19 @@ def _read_codec(codec: JSON) -> JSON:
137137
"""Read a stored codec: the inner chunk shape of a sharding codec is read as a chunk
138138
shape with no known axis lengths (see `_read_chunk_shape`), and so are those of the
139139
sharding codecs nested in its codecs."""
140-
if not (isinstance(codec, Mapping) and codec.get("name") == "sharding_indexed"):
141-
return codec
142-
configuration = codec.get("configuration")
143-
if not isinstance(configuration, Mapping):
144-
return codec
145-
upgraded = dict(configuration)
146-
stored = configuration.get("chunk_shape")
147-
if isinstance(stored, list):
148-
match _read_chunk_shape(stored, [None] * len(stored)):
149-
case chunk_shape, _:
150-
upgraded["chunk_shape"] = list(chunk_shape)
151-
if isinstance(codecs := configuration.get("codecs"), list):
152-
upgraded["codecs"] = [_read_codec(inner) for inner in codecs]
153-
return {**codec, "configuration": upgraded}
140+
match codec:
141+
case {"name": "sharding_indexed", "configuration": Mapping() as configuration}:
142+
upgraded = dict(configuration)
143+
stored = configuration.get("chunk_shape")
144+
if isinstance(stored, list) and (
145+
read := _read_chunk_shape(stored, [None] * len(stored))
146+
):
147+
upgraded["chunk_shape"] = read[0]
148+
if isinstance(codecs := configuration.get("codecs"), list):
149+
upgraded["codecs"] = [_read_codec(inner) for inner in codecs]
150+
# The mapping pattern does not narrow `codec` for mypy.
151+
return {**cast("Mapping[str, JSON]", codec), "configuration": upgraded}
152+
return codec
154153

155154

156155
def _invalid_inner_chunk_sizes_v3(doc: ArrayDocument) -> tuple[ArrayDocument, str | None] | None:
@@ -163,18 +162,16 @@ def _invalid_inner_chunk_sizes_v3(doc: ArrayDocument) -> tuple[ArrayDocument, st
163162
return {**doc, "codecs": codecs}, None
164163

165164

166-
def _inner_chunk_shape(doc: ArrayDocument) -> list[int]:
167-
"""The inner chunk shape of the sharding codec of a Zarr format 3 array document, if
168-
it has one and that is a list of integers; else `[]`."""
169-
codecs = doc.get("codecs")
170-
if isinstance(codecs, list):
171-
for codec in codecs:
172-
match codec:
173-
case {
174-
"name": "sharding_indexed",
175-
"configuration": {"chunk_shape": list() as inner},
176-
}:
177-
return inner if _is_int_list(inner) else []
165+
def _inner_chunk_shape(doc: ArrayDocument) -> list[int] | None:
166+
"""The inner chunk shape of the sharding codec of a Zarr format 3 array document:
167+
`[]` if it has none, `None` if its inner chunk sizes are not all integers of at
168+
least 1 (the unit of its outer chunk shape is then unknown)."""
169+
match doc.get("codecs"):
170+
case list() as codecs:
171+
for codec in codecs:
172+
match codec:
173+
case {"name": "sharding_indexed", "configuration": {"chunk_shape": inner}}:
174+
return inner if _is_int_list(inner) and min(inner, default=1) >= 1 else None
178175
return []
179176

180177

@@ -183,11 +180,13 @@ def _invalid_chunk_sizes_v3(doc: ArrayDocument) -> tuple[ArrayDocument, str | No
183180
shape = doc.get("shape")
184181
if not (isinstance(grid, Mapping) and grid.get("name") == "regular" and _is_int_list(shape)):
185182
return None
183+
if (units := _inner_chunk_shape(doc)) is None:
184+
return None
186185
configuration = grid.get("configuration")
187186
if not isinstance(configuration, Mapping):
188187
return None
189188
stored = configuration.get("chunk_shape")
190-
match _read_chunk_shape(stored, shape, _inner_chunk_shape(doc)):
189+
match _read_chunk_shape(stored, shape, units):
191190
case chunk_shape, reading if not json_equal(chunk_shape, stored):
192191
upgraded = {**configuration, "chunk_shape": chunk_shape}
193192
return {**doc, "chunk_grid": {**grid, "configuration": upgraded}}, reading

‎tests/test_metadata/test_upgrades.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ def test_stored_chunk_shape_ndim_mismatch_rejected() -> None:
255255
_read_strictly(_v3_doc([4, 4], [0]))
256256

257257

258+
@pytest.mark.parametrize("inner", [[0], [False]])
259+
def test_stored_zero_chunk_size_of_shard_with_invalid_inner_chunk_shape_rejected(
260+
inner: list[Any],
261+
) -> None:
262+
"""A stored chunk size of 0 of a sharded array is read in multiples of the inner
263+
chunk size; if that is not an integer of at least 1, the 0 is not upgraded, so it
264+
is rejected."""
265+
with pytest.raises(ValueError, match="^Dimension 0: chunk edge length must be >= 1, got 0$"):
266+
_read_strictly(_v3_doc([4], [0], inner=inner))
267+
268+
258269
def _rectilinear_doc(shape: list[int], chunk_shapes: list[Any]) -> dict[str, JSON]:
259270
return _v3_doc(shape, [1] * len(shape)) | {
260271
"chunk_grid": {

0 commit comments

Comments
 (0)