Skip to content

Commit 8c192b2

Browse files
committed
refactor(chunk-grids): drop dead ndarray branch from is_regular_1d
Since normalize_chunks_nd returns ChunkGrid dimensions instead of int64 arrays, is_regular_1d only ever receives plain Python sequences; the vectorized numpy path was unreachable in production code. Remove it and narrow the signatures of is_regular_1d / is_regular_nd to Sequence[int]. Assisted-by: ClaudeCode:claude-fable-5
1 parent 220d630 commit 8c192b2

2 files changed

Lines changed: 3 additions & 24 deletions

File tree

‎src/zarr/core/chunk_grids.py‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ def _is_rectilinear_chunks(chunks: Any) -> TypeGuard[Sequence[Sequence[int]]]:
334334
return False
335335

336336

337-
def is_regular_1d(
338-
dim_chunks: Sequence[int] | np.ndarray[tuple[int], np.dtype[np.int64]],
339-
) -> bool:
337+
def is_regular_1d(dim_chunks: Sequence[int]) -> bool:
340338
"""Check if a single dimension's chunk sizes represent a regular grid.
341339
342340
A regular dimension has either all chunks the same size, or all
@@ -346,19 +344,14 @@ def is_regular_1d(
346344
if len(dim_chunks) <= 1:
347345
return True
348346
first = dim_chunks[0]
349-
if isinstance(dim_chunks, np.ndarray):
350-
# Vectorized comparison avoids per-element Python iteration over int64 arrays.
351-
return bool((dim_chunks[1:-1] == first).all() and dim_chunks[-1] <= first)
352347
for c in dim_chunks[1:-1]:
353348
if c != first:
354349
return False
355350
# Last chunk must be the same size or a smaller boundary chunk
356351
return dim_chunks[-1] <= first
357352

358353

359-
def is_regular_nd(
360-
chunks: Iterable[Sequence[int] | np.ndarray[tuple[int], np.dtype[np.int64]]],
361-
) -> bool:
354+
def is_regular_nd(chunks: Iterable[Sequence[int]]) -> bool:
362355
"""Check if an N-dimensional chunk specification represents a regular grid."""
363356
return all(is_regular_1d(d) for d in chunks)
364357

‎tests/test_metadata/test_v3.py‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import json
66
from typing import TYPE_CHECKING
77

8-
import numpy as np
98
import pytest
109

1110
from tests.conftest import Expect, ExpectFail
@@ -110,9 +109,6 @@ def test_parse_codecs_unknown_raises(monkeypatch: pytest.MonkeyPatch) -> None:
110109
# Chunk-grid regularity helpers
111110
# ---------------------------------------------------------------------------
112111

113-
# Cases used for both list/tuple (Python-sequence path) and ndarray (vectorized
114-
# path) of `is_regular_1d`. Parametrizing the input form ensures both branches
115-
# are exercised by the same suite of edge cases.
116112
_REGULAR_1D_CASES: list[Expect[list[int], bool]] = [
117113
Expect(input=[], output=True, id="empty"),
118114
Expect(input=[10], output=True, id="single-chunk"),
@@ -129,19 +125,11 @@ def test_parse_codecs_unknown_raises(monkeypatch: pytest.MonkeyPatch) -> None:
129125

130126
@pytest.mark.parametrize("case", _REGULAR_1D_CASES, ids=lambda c: c.id)
131127
def test_is_regular_1d_sequence(case: Expect[list[int], bool]) -> None:
132-
"""`is_regular_1d` accepts plain Python sequences and uses the iterative path."""
133-
# list and tuple both go through the non-ndarray branch.
128+
"""`is_regular_1d` accepts plain Python sequences."""
134129
assert is_regular_1d(case.input) is case.output
135130
assert is_regular_1d(tuple(case.input)) is case.output
136131

137132

138-
@pytest.mark.parametrize("case", _REGULAR_1D_CASES, ids=lambda c: c.id)
139-
def test_is_regular_1d_ndarray(case: Expect[list[int], bool]) -> None:
140-
"""`is_regular_1d` accepts int64 ndarrays and uses the vectorized path."""
141-
arr = np.asarray(case.input, dtype=np.int64)
142-
assert is_regular_1d(arr) is case.output
143-
144-
145133
@pytest.mark.parametrize(
146134
"case",
147135
[
@@ -156,8 +144,6 @@ def test_is_regular_1d_ndarray(case: Expect[list[int], bool]) -> None:
156144
def test_is_regular_nd_sequence(case: Expect[list[list[int]], bool]) -> None:
157145
"""`is_regular_nd` returns True iff every per-dim spec is regular."""
158146
assert is_regular_nd(case.input) is case.output
159-
# Same result via ndarray inputs.
160-
assert is_regular_nd([np.asarray(d, dtype=np.int64) for d in case.input]) is case.output
161147

162148

163149
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)