Skip to content

Commit 087b62d

Browse files
committed
refactor: validate each chunk grid kind once, in one place
Parsing a regular chunk shape repeated its work at two levels: - `_parse_chunk_shape` type-checked and coerced every dimension, then handed the result to `_validate_chunk_shapes`, which re-ran the same isinstance test and coercion and added only the `>= 1` check. Its edge-list branch was unreachable from this caller, which is why a `cast` was needed on the way out. - `RegularChunkGridMetadata.from_dict` parsed the chunk shape and passed it to the constructor, whose `__post_init__` parsed it again. Together that was four passes over the dimensions for `from_dict`. It is now one: `_parse_chunk_shape` checks the range itself and no longer calls the rectilinear validator, and `from_dict` hands the dimensions to the constructor unparsed. Beyond the redundancy, the shared validator was the coupling that let a rectilinear chunk shape be stored as a regular grid (zarr-developers#4374), so the two grid kinds now validate separately. `RectilinearChunkGridMetadata.from_dict` also had its own `>= 1` check for bare-int dimensions, duplicating `_validate_chunk_shapes`, which `__post_init__` runs over the result anyway. It now only puts the JSON into shape (integer vs sequence, RLE expansion), and a bad bare int is reported by the validator, which names the dimension. `expand_rle` keeps its own checks because it is called directly. Assisted-by: ClaudeCode:claude-opus-5
1 parent 38cecdb commit 087b62d

3 files changed

Lines changed: 55 additions & 12 deletions

File tree

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,24 @@ class RectilinearChunkGridMetadataConfig(TypedDict):
218218
def _parse_chunk_shape(chunk_shape: Iterable[int]) -> tuple[int, ...]:
219219
"""Validate and normalize a regular chunk shape.
220220
221-
A regular chunk shape is a sequence of bare ints (one per dimension),
222-
each of which must be >= 1. Per-dimension edge lists belong to a
223-
rectilinear grid and are rejected here.
221+
A regular chunk shape is one bare int per dimension, each >= 1. Lists of
222+
chunk edge lengths belong to a rectilinear chunk grid and are rejected
223+
here; `_validate_chunk_shapes` is the rectilinear counterpart. The two
224+
grid kinds validate separately on purpose — sharing a validator is what
225+
let a rectilinear chunk shape be stored as a regular grid (gh-4374).
224226
"""
225-
normalized: list[int] = []
227+
parsed: list[int] = []
226228
for dim_idx, dim_spec in enumerate(chunk_shape):
227229
if not isinstance(dim_spec, int | np.integer):
228230
raise TypeError(
229231
f"Dimension {dim_idx}: a regular chunk grid requires an integer chunk "
230232
f"edge length, got {dim_spec!r}. Lists of chunk edge lengths belong "
231233
"to a rectilinear chunk grid."
232234
)
233-
normalized.append(int(dim_spec))
234-
result = _validate_chunk_shapes(tuple(normalized))
235-
# Regular grids only have bare ints — cast is safe after validation
236-
return cast(tuple[int, ...], result)
235+
if dim_spec < 1:
236+
raise ValueError(f"Dimension {dim_idx}: chunk size must be >= 1, got {dim_spec}")
237+
parsed.append(int(dim_spec))
238+
return tuple(parsed)
237239

238240

239241
def _validate_chunk_shapes(
@@ -294,7 +296,8 @@ def to_dict(self) -> RegularChunkGridMetadataJSON: # type: ignore[override]
294296
def from_dict(cls, data: RegularChunkGridMetadataJSON) -> Self: # type: ignore[override]
295297
parse_named_configuration(data, "regular") # validate name
296298
configuration = data["configuration"]
297-
return cls(chunk_shape=_parse_chunk_shape(configuration["chunk_shape"]))
299+
# `__post_init__` parses, so this only has to hand over the dimensions.
300+
return cls(chunk_shape=tuple(configuration["chunk_shape"]))
298301

299302

300303
@dataclass(frozen=True, kw_only=True)
@@ -384,8 +387,9 @@ def from_dict(cls, data: RectilinearChunkGridMetadataJSON) -> Self: # type: ign
384387
if declares_chunk_edges(dim_spec):
385388
parsed.append(tuple(expand_rle(dim_spec)))
386389
elif isinstance(dim_spec, int | np.integer):
387-
if dim_spec < 1:
388-
raise ValueError(f"Integer chunk edge length must be >= 1, got {dim_spec}")
390+
# Range checks belong to `_validate_chunk_shapes`, which
391+
# `__post_init__` runs over the result and which names the
392+
# offending dimension.
389393
parsed.append(int(dim_spec))
390394
else:
391395
raise TypeError(

‎tests/test_metadata/test_v3.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
)
3838

3939
if TYPE_CHECKING:
40-
from collections.abc import Sequence
40+
from collections.abc import Callable, Sequence
4141
from typing import Any
4242

4343

@@ -198,6 +198,29 @@ def test_rectilinear_chunk_grid_normalizes_ints(
198198
assert all(type(e) is int for e in flat)
199199

200200

201+
@pytest.mark.parametrize(
202+
"build",
203+
[
204+
pytest.param(lambda shape: RegularChunkGridMetadata(chunk_shape=shape), id="constructor"),
205+
pytest.param(
206+
lambda shape: RegularChunkGridMetadata.from_dict(
207+
{"name": "regular", "configuration": {"chunk_shape": list(shape)}}
208+
),
209+
id="from_dict",
210+
),
211+
],
212+
)
213+
@pytest.mark.parametrize("chunk_shape", [(0, 2), (2, -1)], ids=["zero", "negative"])
214+
def test_regular_chunk_grid_rejects_nonpositive_chunk_size(
215+
build: Callable[[tuple[int, ...]], RegularChunkGridMetadata], chunk_shape: tuple[int, ...]
216+
) -> None:
217+
"""A regular chunk size below 1 is rejected, naming the dimension, whether
218+
the grid is built directly or parsed from stored metadata."""
219+
dim = 0 if chunk_shape[0] < 1 else 1
220+
with pytest.raises(ValueError, match=f"Dimension {dim}: chunk size must be >= 1"):
221+
build(chunk_shape)
222+
223+
201224
def test_regular_chunk_grid_rejects_edge_lists() -> None:
202225
"""A regular chunk grid only accepts integer chunk edge lengths."""
203226
with pytest.raises(TypeError, match="Dimension 1: a regular chunk grid requires an integer"):

‎tests/test_unified_chunk_grid.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,22 @@ def test_serialization_error_non_regular_chunk_shape() -> None:
624624
grid.chunk_shape # noqa: B018
625625

626626

627+
@pytest.mark.parametrize("chunk_shapes", [[0, [5, 5]], [[5, 5], -1]], ids=["zero", "negative"])
628+
def test_rectilinear_from_dict_rejects_nonpositive_bare_int(chunk_shapes: list[Any]) -> None:
629+
"""A bare-int dimension below 1 is rejected by the grid's own validator, which
630+
names the dimension, rather than by a duplicate check in `from_dict`."""
631+
dim = 0 if isinstance(chunk_shapes[0], int) else 1
632+
with pytest.raises(
633+
ValueError, match=f"Dimension {dim}: integer chunk edge length must be >= 1"
634+
):
635+
RectilinearChunkGridMetadata.from_dict(
636+
{
637+
"name": "rectilinear",
638+
"configuration": {"kind": "inline", "chunk_shapes": chunk_shapes},
639+
}
640+
)
641+
642+
627643
def test_serialization_error_zero_extent_rectilinear() -> None:
628644
"""RectilinearChunkGridMetadata rejects empty edge tuples."""
629645
with pytest.raises(ValueError, match="has no chunk edges"):

0 commit comments

Comments
 (0)