Skip to content

Commit fcd5ab0

Browse files
committed
fix: don't accept inane input
1 parent 88e93ad commit fcd5ab0

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

src/zarr/core/chunk_grids.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -680,17 +680,17 @@ def _guess_regular_chunks(
680680
return tuple(int(x) for x in chunks)
681681

682682

683-
def normalize_chunks_1d(chunks: int | None | Iterable[int], span: int) -> tuple[int, ...]:
683+
def normalize_chunks_1d(chunks: int | Iterable[int], span: int) -> tuple[int, ...]:
684684
"""
685685
Normalize a one-dimensional chunk specification into a tuple of logical
686686
chunk sizes that cover the span.
687687
688-
`None` and `-1` both mean "one chunk covering the entire span."
688+
`-1` means "one chunk covering the entire span."
689689
For an integer chunk size, all chunks are uniform — the last chunk may
690690
overhang the span. The actual data extent of each chunk is determined
691691
by the chunk grid at runtime, not by this function.
692692
"""
693-
if chunks is None or chunks == -1:
693+
if chunks == -1:
694694
return (span,)
695695
if isinstance(chunks, int):
696696
if chunks <= 0:
@@ -738,14 +738,13 @@ def normalize_chunks_nd(
738738
chunks = tuple(int(chunks) for _ in shape)
739739

740740
# handle bad dimensionality
741-
if len(chunks) > len(shape):
742-
raise ValueError("too many dimensions in chunks")
743-
744-
# pad short specs with None (meaning "use span") for remaining dimensions
745-
padded = tuple(chunks) + (None,) * (len(shape) - len(chunks))
741+
if len(chunks) != len(shape):
742+
raise ValueError(
743+
f"chunks has {len(chunks)} dimensions but shape has {len(shape)} dimensions"
744+
)
746745

747746
return ChunksTuple(
748-
tuple(normalize_chunks_1d(c, span=s) for c, s in zip(padded, shape, strict=True))
747+
tuple(normalize_chunks_1d(c, span=s) for c, s in zip(chunks, shape, strict=True))
749748
)
750749

751750

tests/test_chunk_grids.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
3434
# 2D cases
3535
((10, 10), (100, 10), ((10,) * 10, (10,))),
3636
(10, (100, 10), ((10,) * 10, (10,))),
37-
((10, None), (100, 10), ((10,) * 10, (10,))),
37+
((10, -1), (100, 10), ((10,) * 10, (10,))),
3838
# 3D cases
3939
(30, (100, 20, 10), ((30, 30, 30, 30), (30,), (30,))),
40-
((30,), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
41-
((30, None), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
42-
((30, None, None), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
43-
((30, 20, None), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
40+
((30, -1, -1), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
41+
((30, 20, -1), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
4442
((30, 20, 10), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
4543
# dask-style chunks (explicit per-chunk sizes)
4644
(((100, 100, 100), (50, 50)), (300, 100), ((100, 100, 100), (50, 50))),
@@ -51,7 +49,6 @@ def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
5149
(False, (100, 50), ((100,), (50,))),
5250
# sentinel values
5351
(-1, (100,), ((100,),)),
54-
((30, -1, None), (100, 20, 10), ((30, 30, 30, 30), (20,), (10,))),
5552
# zero-length dimensions preserve the declared chunk size
5653
(10, (0,), ((10,),)),
5754
((5, 10), (0, 100), ((5,), (10,) * 10)),
@@ -97,5 +94,7 @@ def test_normalize_chunks_errors() -> None:
9794
normalize_chunks_nd(None, (100,))
9895
with pytest.raises(ValueError):
9996
normalize_chunks_nd("foo", (100,))
100-
with pytest.raises(ValueError):
97+
with pytest.raises(ValueError, match="dimensions"):
10198
normalize_chunks_nd((100, 10), (100,))
99+
with pytest.raises(ValueError, match="dimensions"):
100+
normalize_chunks_nd((10,), (100, 100))

tests/test_codecs/test_sharding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def test_invalid_shard_shape() -> None:
497497
{},
498498
shape=(16, 16),
499499
shards=(16, 16),
500-
chunks=(9,),
500+
chunks=(9, 9),
501501
dtype=np.dtype("uint8"),
502502
fill_value=0,
503503
)

0 commit comments

Comments
 (0)