Skip to content

Commit 3d53725

Browse files
jhammanclaude
andcommitted
Address PR zarr-developers#3534 review comments
- Fix _info() crash for RectilinearChunkGrid arrays by catching NotImplementedError from metadata.chunks (displays "<variable>") - Fix spurious FutureWarning in _nchunks_initialized() by using array.metadata.chunks instead of array.chunks - Add empty-chunk validation in _normalize_rectilinear_chunks to reject chunk specs where the last chunk(s) contain no valid data - Remove FutureWarning deprecation on .chunks for RegularChunkGrid per reviewer feedback; .chunks still raises NotImplementedError for RectilinearChunkGrid Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 629d34a commit 3d53725

3 files changed

Lines changed: 19 additions & 32 deletions

File tree

src/zarr/core/array.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,15 +1070,11 @@ def chunk_grid(self) -> ChunkGrid:
10701070
def chunks(self) -> tuple[int, ...]:
10711071
"""Returns the chunk shape of the Array.
10721072
1073-
.. deprecated::
1074-
The `chunks` property is deprecated and will be removed in a future version.
1075-
Use `chunk_grid` instead to access chunk information.
1076-
10771073
For arrays using RegularChunkGrid: returns a tuple of ints with uniform chunk sizes.
10781074
If sharding is used, the inner chunk shape is returned.
10791075
10801076
For arrays using RectilinearChunkGrid: raises NotImplementedError as variable chunks
1081-
cannot be represented as a simple tuple.
1077+
cannot be represented as a simple tuple. Use ``chunk_grid`` instead.
10821078
10831079
Returns
10841080
-------
@@ -1096,12 +1092,6 @@ def chunks(self) -> tuple[int, ...]:
10961092
"(RectilinearChunkGrid). Use `chunk_grid` instead to access chunk information."
10971093
)
10981094
raise NotImplementedError(msg)
1099-
warnings.warn(
1100-
"Array.chunks is deprecated and will be removed in a future version. "
1101-
"Use Array.chunk_grid.chunk_shape instead.",
1102-
FutureWarning,
1103-
stacklevel=2,
1104-
)
11051095
return self.metadata.chunks
11061096

11071097
@property
@@ -1990,14 +1980,18 @@ async def info_complete(self) -> Any:
19901980
def _info(
19911981
self, count_chunks_initialized: int | None = None, count_bytes_stored: int | None = None
19921982
) -> Any:
1983+
try:
1984+
chunk_shape: tuple[int, ...] | None = self.metadata.chunks
1985+
except NotImplementedError:
1986+
chunk_shape = None
19931987
return ArrayInfo(
19941988
_zarr_format=self.metadata.zarr_format,
19951989
_data_type=self._zdtype,
19961990
_fill_value=self.metadata.fill_value,
19971991
_shape=self.shape,
19981992
_order=self.order,
19991993
_shard_shape=self.shards,
2000-
_chunk_shape=self.chunks,
1994+
_chunk_shape=chunk_shape,
20011995
_read_only=self.read_only,
20021996
_compressors=self.compressors,
20031997
_filters=self.filters,
@@ -2342,15 +2336,11 @@ def chunk_grid(self) -> ChunkGrid:
23422336
def chunks(self) -> tuple[int, ...]:
23432337
"""Returns the chunk shape of the Array.
23442338
2345-
.. deprecated::
2346-
The `chunks` property is deprecated and will be removed in a future version.
2347-
Use `chunk_grid` instead to access chunk information.
2348-
23492339
For arrays using RegularChunkGrid: returns a tuple of ints with uniform chunk sizes.
23502340
If sharding is used, the inner chunk shape is returned.
23512341
23522342
For arrays using RectilinearChunkGrid: raises NotImplementedError as variable chunks
2353-
cannot be represented as a simple tuple.
2343+
cannot be represented as a simple tuple. Use ``chunk_grid`` instead.
23542344
23552345
Returns
23562346
-------
@@ -5734,7 +5724,7 @@ async def _nchunks_initialized(
57345724
chunks_per_shard = 1
57355725
else:
57365726
chunks_per_shard = product(
5737-
tuple(a // b for a, b in zip(array.shards, array.chunks, strict=True))
5727+
tuple(a // b for a, b in zip(array.shards, array.metadata.chunks, strict=True))
57385728
)
57395729
return (await _nshards_initialized(array)) * chunks_per_shard
57405730

src/zarr/core/chunk_grids.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@ def _normalize_rectilinear_chunks(
13971397
f"Variable chunks along dimension {i} sum to {chunk_sum} "
13981398
f"but array shape is {dim_size}. Chunks must sum to be greater than or equal to the shape."
13991399
)
1400+
if sum(dim_chunks[:-1]) >= dim_size:
1401+
raise ValueError(
1402+
f"Dimension {i} has more chunks than needed. "
1403+
f"The last chunk(s) would contain no valid data. "
1404+
f"Remove the extra chunk(s) or increase the array shape."
1405+
)
14001406

14011407
return chunk_shapes
14021408

tests/test_chunk_grids/test_rectilinear.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,8 @@ def test_sync_array_chunks_property_rectilinear_raises() -> None:
13261326
assert chunk_grid.chunk_shapes == ((10, 20, 30, 40), (25, 25, 50))
13271327

13281328

1329-
async def test_array_chunks_property_regular_with_warning() -> None:
1330-
"""Test that Array.chunks emits FutureWarning for RegularChunkGrid."""
1331-
import warnings
1332-
1329+
async def test_array_chunks_property_regular() -> None:
1330+
"""Test that Array.chunks works for RegularChunkGrid."""
13331331
store = MemoryStore()
13341332

13351333
arr = await zarr.api.asynchronous.create_array(
@@ -1340,20 +1338,13 @@ async def test_array_chunks_property_regular_with_warning() -> None:
13401338
zarr_format=3,
13411339
)
13421340

1343-
# chunks should emit FutureWarning for regular grids
1344-
with warnings.catch_warnings(record=True) as w:
1345-
warnings.simplefilter("always")
1346-
chunks = arr.chunks
1347-
assert len(w) == 1
1348-
assert issubclass(w[0].category, FutureWarning)
1349-
assert "deprecated" in str(w[0].message)
1350-
1351-
# For regular chunks, it's tuple[int, ...]
1341+
# For regular chunks, returns tuple[int, ...]
1342+
chunks = arr.chunks
13521343
assert chunks == (10, 20)
13531344
assert isinstance(chunks[0], int)
13541345
assert isinstance(chunks[1], int)
13551346

1356-
# chunk_grid.chunk_shape works without warning
1347+
# chunk_grid.chunk_shape also works
13571348
chunk_grid = arr.chunk_grid
13581349
assert isinstance(chunk_grid, RegularChunkGrid)
13591350
assert chunk_grid.chunk_shape == (10, 20)

0 commit comments

Comments
 (0)