Skip to content

Commit 2c06fb2

Browse files
authored
chore: simplify sharding codec validation against varying chunk grid metadata (#7)
* chore: simplify sharding codec validation against varying chunk grid metadata * test: restore test strength
1 parent f2ec718 commit 2c06fb2

2 files changed

Lines changed: 12 additions & 27 deletions

File tree

src/zarr/codecs/sharding.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -394,35 +394,21 @@ def validate(
394394
"The shard's `chunk_shape` and array's `shape` need to have the same number of dimensions."
395395
)
396396
if isinstance(chunk_grid, RegularChunkGrid):
397-
if not all(
398-
s % c == 0
399-
for s, c in zip(
400-
chunk_grid.chunk_shape,
401-
self.chunk_shape,
402-
strict=False,
403-
)
404-
):
405-
raise ValueError(
406-
f"The array's `chunk_shape` (got {chunk_grid.chunk_shape}) "
407-
f"needs to be divisible by the shard's inner `chunk_shape` (got {self.chunk_shape})."
408-
)
397+
edges_per_dim: tuple[tuple[int, ...], ...] = tuple((s,) for s in chunk_grid.chunk_shape)
409398
elif isinstance(chunk_grid, RectilinearChunkGrid):
410-
# For rectilinear grids, every unique edge length per dimension
411-
# must be divisible by the corresponding inner chunk size.
412-
for i, (edges, inner) in enumerate(
413-
zip(chunk_grid.chunk_shapes, self.chunk_shape, strict=False)
414-
):
415-
for edge in set(edges):
416-
if edge % inner != 0:
417-
raise ValueError(
418-
f"Chunk edge length {edge} in dimension {i} is not "
419-
f"divisible by the shard's inner chunk size {inner}."
420-
)
399+
edges_per_dim = chunk_grid.chunk_shapes
421400
else:
422401
raise TypeError(
423402
f"Sharding is only compatible with regular and rectilinear chunk grids, "
424403
f"got {type(chunk_grid)}"
425404
)
405+
for i, (edges, inner) in enumerate(zip(edges_per_dim, self.chunk_shape, strict=False)):
406+
for edge in set(edges):
407+
if edge % inner != 0:
408+
raise ValueError(
409+
f"Chunk edge length {edge} in dimension {i} is not "
410+
f"divisible by the shard's inner chunk size {inner}."
411+
)
426412

427413
async def _decode_single(
428414
self,

tests/test_codecs/test_sharding.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pickle
2-
import re
32
from typing import Any
43

54
import numpy as np
@@ -489,9 +488,9 @@ def test_invalid_metadata(store: Store) -> None:
489488
def test_invalid_shard_shape() -> None:
490489
with pytest.raises(
491490
ValueError,
492-
match=re.escape(
493-
"The array's `chunk_shape` (got (16, 16)) needs to be divisible "
494-
"by the shard's inner `chunk_shape` (got (9,))."
491+
match=(
492+
f"Chunk edge length {16} in dimension {0} is not "
493+
f"divisible by the shard's inner chunk size {9}\\."
495494
),
496495
):
497496
zarr.create_array(

0 commit comments

Comments
 (0)