Skip to content

Commit 23724cc

Browse files
committed
fixups
1 parent 6a78985 commit 23724cc

5 files changed

Lines changed: 48 additions & 18 deletions

File tree

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _get_shape_chunks(a: ArrayLike | Any) -> tuple[tuple[int, ...] | None, tuple
123123
# bcolz carray
124124
chunks = (a.chunklen,) + a.shape[1:]
125125

126-
return shape, chunks # type: ignore[return-value]
126+
return shape, chunks
127127

128128

129129
class _LikeArgs(TypedDict):

src/zarr/core/array.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,13 @@ def _shard_grid_shape(self) -> tuple[int, ...]:
13261326
The shape of the shard grid for this array.
13271327
"""
13281328
if self.shards is None:
1329-
shard_shape = self.metadata.chunk_grid.chunk_shape
1329+
chunk_grid = self.metadata.chunk_grid
1330+
if not isinstance(chunk_grid, RegularChunkGrid):
1331+
raise NotImplementedError(
1332+
"shard_shape is not supported for arrays with variable chunk sizes "
1333+
"(RectilinearChunkGrid)."
1334+
)
1335+
shard_shape = chunk_grid.chunk_shape
13301336
else:
13311337
shard_shape = self.shards
13321338
return tuple(starmap(ceildiv, zip(self.shape, shard_shape, strict=True)))
@@ -1402,10 +1408,11 @@ async def example():
14021408
if self.shards is None:
14031409
chunks_per_shard = 1
14041410
else:
1405-
# Sharding only applies to RegularChunkGrid, so chunks is tuple[int, ...]
1406-
chunks = cast(tuple[int, ...], self.chunks)
1411+
# Sharding only applies to RegularChunkGrid
1412+
# Use metadata.chunks to get the inner chunk shape when sharding is used
1413+
chunk_shape = self.metadata.chunks
14071414
chunks_per_shard = product(
1408-
tuple(a // b for a, b in zip(self.shards, chunks, strict=True))
1415+
tuple(a // b for a, b in zip(self.shards, chunk_shape, strict=True))
14091416
)
14101417
return (await self._nshards_initialized()) * chunks_per_shard
14111418

@@ -5221,7 +5228,15 @@ def _parse_keep_array_attr(
52215228
]:
52225229
if isinstance(data, Array):
52235230
if chunks == "keep":
5224-
chunks = data.chunks # type: ignore[assignment]
5231+
# Get the chunk shape(s) from the chunk_grid
5232+
chunk_grid = data.chunk_grid
5233+
if isinstance(chunk_grid, RegularChunkGrid):
5234+
chunks = chunk_grid.chunk_shape
5235+
elif isinstance(chunk_grid, RectilinearChunkGrid):
5236+
chunks = chunk_grid.chunk_shapes # type: ignore[assignment]
5237+
else:
5238+
msg = f"Unsupported chunk grid type: {type(chunk_grid).__name__}"
5239+
raise TypeError(msg)
52255240
if shards == "keep":
52265241
shards = data.shards
52275242
if zarr_format is None:
@@ -5701,14 +5716,15 @@ def _iter_shard_regions(
57015716
If the array uses RectilinearChunkGrid (variable-sized chunks).
57025717
when no shards are present.
57035718
"""
5704-
if isinstance(array.metadata.chunk_grid, RectilinearChunkGrid):
5719+
chunk_grid = array.metadata.chunk_grid
5720+
if not isinstance(chunk_grid, RegularChunkGrid):
57055721
raise NotImplementedError(
57065722
"_iter_shard_regions is not supported for arrays with variable-sized chunks "
57075723
"(RectilinearChunkGrid). Use the chunk_grid API directly for variable chunk access."
57085724
)
57095725

57105726
if array.shards is None:
5711-
shard_shape: Sequence[int] = array.metadata.chunk_grid.chunk_shape
5727+
shard_shape: Sequence[int] = chunk_grid.chunk_shape
57125728
else:
57135729
shard_shape = array.shards
57145730

@@ -5747,15 +5763,16 @@ def _iter_chunk_regions(
57475763
NotImplementedError
57485764
If the array uses RectilinearChunkGrid (variable-sized chunks).
57495765
"""
5750-
if isinstance(array.metadata.chunk_grid, RectilinearChunkGrid):
5766+
chunk_grid = array.metadata.chunk_grid
5767+
if not isinstance(chunk_grid, RegularChunkGrid):
57515768
raise NotImplementedError(
57525769
"_iter_chunk_regions is not supported for arrays with variable-sized chunks "
57535770
"(RectilinearChunkGrid). Use the chunk_grid API directly for variable chunk access."
57545771
)
57555772

57565773
return _iter_regions(
57575774
array.shape,
5758-
array.metadata.chunk_grid.chunk_shape,
5775+
chunk_grid.chunk_shape,
57595776
origin=origin,
57605777
selection_shape=selection_shape,
57615778
trim_excess=True,

src/zarr/core/chunk_grids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numbers
77
import operator
88
import warnings
9-
from abc import ABC, abstractmethod
9+
from abc import abstractmethod
1010
from collections.abc import Sequence
1111
from dataclasses import dataclass, replace
1212
from functools import cached_property, reduce

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async def test_array_like_creation(
154154
kwargs["chunks"] = out_chunks
155155
expect_chunks = out_chunks
156156
else:
157-
expect_chunks = ref_arr.chunks # type: ignore[assignment]
157+
expect_chunks = ref_arr.chunks
158158
if out_dtype != "keep":
159159
kwargs["dtype"] = out_dtype
160160
expect_dtype = out_dtype

tests/test_chunk_grids/test_rectilinear.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import zarr
1010
from zarr.core.chunk_grids import (
1111
RectilinearChunkGrid,
12+
RegularChunkGrid,
1213
_compress_run_length_encoding,
1314
_expand_run_length_encoding,
1415
_parse_chunk_shapes,
@@ -1280,7 +1281,9 @@ async def test_array_chunks_property_rectilinear_raises() -> None:
12801281
_ = arr.chunks
12811282

12821283
# Use chunk_grid instead
1283-
assert arr.chunk_grid.chunk_shapes == ((10, 20, 30), (25, 25))
1284+
chunk_grid = arr.chunk_grid
1285+
assert isinstance(chunk_grid, RectilinearChunkGrid)
1286+
assert chunk_grid.chunk_shapes == ((10, 20, 30), (25, 25))
12841287

12851288

12861289
async def test_array_chunk_grid_property_rectilinear() -> None:
@@ -1318,7 +1321,9 @@ def test_sync_array_chunks_property_rectilinear_raises() -> None:
13181321
_ = arr.chunks
13191322

13201323
# Use chunk_grid instead
1321-
assert arr.chunk_grid.chunk_shapes == ((10, 20, 30, 40), (25, 25, 50))
1324+
chunk_grid = arr.chunk_grid
1325+
assert isinstance(chunk_grid, RectilinearChunkGrid)
1326+
assert chunk_grid.chunk_shapes == ((10, 20, 30, 40), (25, 25, 50))
13221327

13231328

13241329
async def test_array_chunks_property_regular_with_warning() -> None:
@@ -1349,7 +1354,9 @@ async def test_array_chunks_property_regular_with_warning() -> None:
13491354
assert isinstance(chunks[1], int)
13501355

13511356
# chunk_grid.chunk_shape works without warning
1352-
assert arr.chunk_grid.chunk_shape == (10, 20)
1357+
chunk_grid = arr.chunk_grid
1358+
assert isinstance(chunk_grid, RegularChunkGrid)
1359+
assert chunk_grid.chunk_shape == (10, 20)
13531360

13541361

13551362
async def test_array_chunk_grid_after_resize() -> None:
@@ -1364,13 +1371,17 @@ async def test_array_chunk_grid_after_resize() -> None:
13641371
zarr_format=3,
13651372
)
13661373

1367-
assert arr.chunk_grid.chunk_shapes == ((10, 20), (15, 15))
1374+
chunk_grid = arr.chunk_grid
1375+
assert isinstance(chunk_grid, RectilinearChunkGrid)
1376+
assert chunk_grid.chunk_shapes == ((10, 20), (15, 15))
13681377

13691378
# Resize to grow
13701379
await arr.resize((50, 45))
13711380

13721381
# Chunk grid should be updated
1373-
assert arr.chunk_grid.chunk_shapes == ((10, 20, 20), (15, 15, 15))
1382+
chunk_grid = arr.chunk_grid
1383+
assert isinstance(chunk_grid, RectilinearChunkGrid)
1384+
assert chunk_grid.chunk_shapes == ((10, 20, 20), (15, 15, 15))
13741385

13751386

13761387
async def test_metadata_chunks_property_raises_for_rectilinear() -> None:
@@ -1390,7 +1401,9 @@ async def test_metadata_chunks_property_raises_for_rectilinear() -> None:
13901401
_ = arr.metadata.chunks
13911402

13921403
# Use chunk_grid instead
1393-
assert arr.metadata.chunk_grid.chunk_shapes == ((10, 20, 30), (25, 25))
1404+
chunk_grid = arr.metadata.chunk_grid
1405+
assert isinstance(chunk_grid, RectilinearChunkGrid)
1406+
assert chunk_grid.chunk_shapes == ((10, 20, 30), (25, 25))
13941407

13951408

13961409
# ===================================================================

0 commit comments

Comments
 (0)