Skip to content

Commit 6a78985

Browse files
committed
revert changes to .chunks property / chunks-type
1 parent 84f47d3 commit 6a78985

8 files changed

Lines changed: 158 additions & 427 deletions

File tree

docs/user-guide/arrays.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,13 @@ print(f"Slice [10:30, 50:75]: {z[10:30, 50:75].shape}")
617617
### Accessing chunk information
618618

619619
With variable chunking, the standard `.chunks` property is not available since chunks
620-
have different sizes. Instead, access chunk information through the chunk grid:
620+
have different sizes. Instead, access chunk information through the `.chunk_grid` property:
621621

622622
```python exec="true" session="arrays" source="above" result="ansi"
623623
from zarr.core.chunk_grids import RectilinearChunkGrid
624624

625625
# Access the chunk grid
626-
chunk_grid = z.metadata.chunk_grid
626+
chunk_grid = z.chunk_grid
627627
print(f"Chunk grid type: {type(chunk_grid).__name__}")
628628

629629
# Get chunk shapes for each dimension
@@ -659,8 +659,8 @@ z_timeseries = zarr.create_array(
659659
zarr_format=3
660660
)
661661
print(f"Created array with shape {z_timeseries.shape}")
662-
print(f"Chunk shapes: {z_timeseries.metadata.chunk_grid.chunk_shapes}")
663-
print(f"Number of chunks: {len(z_timeseries.metadata.chunk_grid.chunk_shapes[0])} months")
662+
print(f"Chunk shapes: {z_timeseries.chunk_grid.chunk_shapes}")
663+
print(f"Number of chunks: {len(z_timeseries.chunk_grid.chunk_shapes[0])} months")
664664
```
665665

666666
### Limitations
@@ -678,8 +678,7 @@ Variable chunking has some important limitations:
678678
to partition the input data, which requires regular chunk sizes.
679679

680680
4. **No `.chunks` property**: For arrays with variable chunking, accessing the `.chunks`
681-
property will raise a `NotImplementedError`. Use `.metadata.chunk_grid.chunk_shapes`
682-
instead.
681+
property will raise a `NotImplementedError`. Use `.chunk_grid.chunk_shapes` instead.
683682

684683
## Missing features in 3.0
685684

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ addopts = [
410410
filterwarnings = [
411411
"error",
412412
"ignore:Unclosed client session <aiohttp.client.ClientSession.*:ResourceWarning",
413-
"ignore:Numcodecs codecs are not in the Zarr version 3 specification.*:UserWarning"
413+
"ignore:Numcodecs codecs are not in the Zarr version 3 specification.*:UserWarning",
414+
"ignore:Array.chunks is deprecated and will be removed in a future version.*:FutureWarning"
414415
]
415416
markers = [
416417
"asyncio: mark test as asyncio test",

src/zarr/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
zeros_like,
3636
)
3737
from zarr.core.array import Array, AsyncArray
38-
from zarr.core.chunk_grids import ChunksType, RectilinearChunks, RegularChunks
3938
from zarr.core.config import config
4039
from zarr.core.group import AsyncGroup, Group
4140

@@ -148,10 +147,7 @@ def set_format(log_format: str) -> None:
148147
"Array",
149148
"AsyncArray",
150149
"AsyncGroup",
151-
"ChunksType",
152150
"Group",
153-
"RectilinearChunks",
154-
"RegularChunks",
155151
"__version__",
156152
"array",
157153
"config",

src/zarr/core/array.py

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
from zarr.core.chunk_grids import (
4444
ChunkGrid,
4545
ChunksLike,
46-
ChunksType,
46+
RectilinearChunkGrid,
4747
RegularChunkGrid,
48-
RegularChunks,
4948
_auto_partition,
5049
_normalize_chunks,
5150
resolve_chunk_spec,
@@ -1052,34 +1051,52 @@ def shape(self) -> tuple[int, ...]:
10521051
return self.metadata.shape
10531052

10541053
@property
1055-
def chunks(self) -> ChunksType:
1056-
"""Returns the chunk specification of the Array.
1054+
def chunk_grid(self) -> ChunkGrid:
1055+
"""Returns the chunk grid of the Array.
10571056
1058-
Returns either RegularChunks (for uniform chunk sizes) or RectilinearChunks
1059-
(for variable chunk sizes per dimension). Both types behave like tuples but
1060-
provide richer semantics including named access when dimension_names are available.
1057+
Returns
1058+
-------
1059+
ChunkGrid
1060+
The chunk grid (RegularChunkGrid or RectilinearChunkGrid)
1061+
"""
1062+
return self.metadata.chunk_grid
1063+
1064+
@property
1065+
def chunks(self) -> tuple[int, ...]:
1066+
"""Returns the chunk shape of the Array.
1067+
1068+
.. deprecated::
1069+
The `chunks` property is deprecated and will be removed in a future version.
1070+
Use `chunk_grid` instead to access chunk information.
10611071
1062-
For arrays using RegularChunkGrid: returns RegularChunks with uniform chunk sizes.
1072+
For arrays using RegularChunkGrid: returns a tuple of ints with uniform chunk sizes.
10631073
If sharding is used, the inner chunk shape is returned.
10641074
1065-
For arrays using RectilinearChunkGrid: returns RectilinearChunks where each
1066-
element is a tuple containing the chunk sizes along that dimension (not RLE encoded).
1075+
For arrays using RectilinearChunkGrid: raises NotImplementedError as variable chunks
1076+
cannot be represented as a simple tuple.
10671077
10681078
Returns
10691079
-------
1070-
ChunksType
1071-
RegularChunks for regular chunks or RectilinearChunks for variable chunks
1080+
tuple[int, ...]
1081+
The chunk shape for regular chunk grids
10721082
1073-
Examples
1074-
--------
1075-
>>> arr = zarr.create_array(shape=(100, 200), chunks=(10, 20))
1076-
>>> arr.chunks
1077-
RegularChunks((10, 20))
1078-
>>> isinstance(arr.chunks, RegularChunks)
1079-
True
1080-
>>> tuple(arr.chunks)
1081-
(10, 20)
1083+
Raises
1084+
------
1085+
NotImplementedError
1086+
If the array uses RectilinearChunkGrid (variable chunks)
10821087
"""
1088+
if isinstance(self.metadata.chunk_grid, RectilinearChunkGrid):
1089+
msg = (
1090+
"The `chunks` property is not supported for arrays with variable chunk sizes "
1091+
"(RectilinearChunkGrid). Use `chunk_grid` instead to access chunk information."
1092+
)
1093+
raise NotImplementedError(msg)
1094+
warnings.warn(
1095+
"Array.chunks is deprecated and will be removed in a future version. "
1096+
"Use Array.chunk_grid.chunk_shape instead.",
1097+
FutureWarning,
1098+
stacklevel=2,
1099+
)
10831100
return self.metadata.chunks
10841101

10851102
@property
@@ -1285,15 +1302,16 @@ def _chunk_grid_shape(self) -> tuple[int, ...]:
12851302
tuple[int, ...]
12861303
The shape of the chunk grid for this array.
12871304
"""
1288-
chunks = self.chunks
1305+
# For RectilinearChunkGrid, use the chunk_grid method
1306+
if isinstance(self.metadata.chunk_grid, RectilinearChunkGrid):
1307+
return self.metadata.chunk_grid.get_chunk_grid_shape(self.shape)
1308+
# For RegularChunkGrid (including sharded), use metadata.chunks which
1309+
# correctly returns the inner chunk shape for sharded arrays
1310+
chunk_shape = self.metadata.chunks
12891311
# Handle 0-dimensional arrays
1290-
if len(chunks) == 0:
1312+
if len(chunk_shape) == 0:
12911313
return ()
1292-
# For RegularChunkGrid (or sharded), use RegularChunks type check
1293-
if isinstance(chunks, RegularChunks):
1294-
return tuple(starmap(ceildiv, zip(self.shape, chunks, strict=True)))
1295-
# For RectilinearChunkGrid, use the chunk_grid method
1296-
return self.metadata.chunk_grid.get_chunk_grid_shape(self.shape)
1314+
return tuple(starmap(ceildiv, zip(self.shape, chunk_shape, strict=True)))
12971315

12981316
@property
12991317
def _shard_grid_shape(self) -> tuple[int, ...]:
@@ -1308,7 +1326,7 @@ def _shard_grid_shape(self) -> tuple[int, ...]:
13081326
The shape of the shard grid for this array.
13091327
"""
13101328
if self.shards is None:
1311-
shard_shape: tuple[int, ...] | ChunksType = self.chunks
1329+
shard_shape = self.metadata.chunk_grid.chunk_shape
13121330
else:
13131331
shard_shape = self.shards
13141332
return tuple(starmap(ceildiv, zip(self.shape, shard_shape, strict=True)))
@@ -2396,20 +2414,39 @@ def shape(self, value: tuple[int, ...]) -> None:
23962414
self.resize(value)
23972415

23982416
@property
2399-
def chunks(self) -> tuple[int, ...] | tuple[tuple[int, ...], ...]:
2400-
"""Returns the chunk specification of the Array.
2417+
def chunk_grid(self) -> ChunkGrid:
2418+
"""Returns the chunk grid of the Array.
2419+
2420+
Returns
2421+
-------
2422+
ChunkGrid
2423+
The chunk grid (RegularChunkGrid or RectilinearChunkGrid)
2424+
"""
2425+
return self.async_array.chunk_grid
2426+
2427+
@property
2428+
def chunks(self) -> tuple[int, ...]:
2429+
"""Returns the chunk shape of the Array.
24012430
2402-
For arrays using RegularChunkGrid: returns a tuple of ints representing
2403-
the uniform chunk shape. If sharding is used, the inner chunk shape is returned.
2431+
.. deprecated::
2432+
The `chunks` property is deprecated and will be removed in a future version.
2433+
Use `chunk_grid` instead to access chunk information.
24042434
2405-
For arrays using RectilinearChunkGrid: returns a tuple of tuples, where
2406-
each inner tuple contains the chunk sizes along that dimension (not RLE encoded).
2435+
For arrays using RegularChunkGrid: returns a tuple of ints with uniform chunk sizes.
2436+
If sharding is used, the inner chunk shape is returned.
2437+
2438+
For arrays using RectilinearChunkGrid: raises NotImplementedError as variable chunks
2439+
cannot be represented as a simple tuple.
24072440
24082441
Returns
24092442
-------
2410-
tuple[int, ...] | tuple[tuple[int, ...], ...]
2411-
For regular chunks: (chunk_size_dim0, chunk_size_dim1, ...)
2412-
For rectilinear chunks: ((sizes_dim0), (sizes_dim1), ...)
2443+
tuple[int, ...]
2444+
The chunk shape for regular chunk grids
2445+
2446+
Raises
2447+
------
2448+
NotImplementedError
2449+
If the array uses RectilinearChunkGrid (variable chunks)
24132450
"""
24142451
return self.async_array.chunks
24152452

@@ -5664,16 +5701,14 @@ def _iter_shard_regions(
56645701
If the array uses RectilinearChunkGrid (variable-sized chunks).
56655702
when no shards are present.
56665703
"""
5667-
chunks = array.chunks
5668-
if not isinstance(chunks, RegularChunks):
5704+
if isinstance(array.metadata.chunk_grid, RectilinearChunkGrid):
56695705
raise NotImplementedError(
56705706
"_iter_shard_regions is not supported for arrays with variable-sized chunks "
56715707
"(RectilinearChunkGrid). Use the chunk_grid API directly for variable chunk access."
56725708
)
56735709

5674-
# Type narrowing: chunks is now RegularChunks (subclass of tuple[int, ...])
56755710
if array.shards is None:
5676-
shard_shape: Sequence[int] = chunks
5711+
shard_shape: Sequence[int] = array.metadata.chunk_grid.chunk_shape
56775712
else:
56785713
shard_shape = array.shards
56795714

@@ -5712,17 +5747,15 @@ def _iter_chunk_regions(
57125747
NotImplementedError
57135748
If the array uses RectilinearChunkGrid (variable-sized chunks).
57145749
"""
5715-
chunks = array.chunks
5716-
if not isinstance(chunks, RegularChunks):
5750+
if isinstance(array.metadata.chunk_grid, RectilinearChunkGrid):
57175751
raise NotImplementedError(
57185752
"_iter_chunk_regions is not supported for arrays with variable-sized chunks "
57195753
"(RectilinearChunkGrid). Use the chunk_grid API directly for variable chunk access."
57205754
)
57215755

5722-
# Type narrowing: chunks is now RegularChunks (subclass of tuple[int, ...])
57235756
return _iter_regions(
57245757
array.shape,
5725-
chunks,
5758+
array.metadata.chunk_grid.chunk_shape,
57265759
origin=origin,
57275760
selection_shape=selection_shape,
57285761
trim_excess=True,

0 commit comments

Comments
 (0)