From da9885cdf4da16a4b52695b5522bc42982bb2f41 Mon Sep 17 00:00:00 2001 From: Ryan Abernathey Date: Mon, 3 Jun 2024 14:07:44 -0400 Subject: [PATCH] implement .chunks on v3 arrays (#1929) * implement .chunks on v3 arrays * remove noqa: B009 * make mypy happy * only return chunks for regular chunk grids --------- Co-authored-by: Davis Bennett Co-authored-by: Joseph Hamman --- src/zarr/array.py | 13 +++++++++++++ tests/v3/test_group.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/zarr/array.py b/src/zarr/array.py index 3e6cf5531..698894ba0 100644 --- a/src/zarr/array.py +++ b/src/zarr/array.py @@ -366,6 +366,15 @@ def ndim(self) -> int: def shape(self) -> ChunkCoords: return self.metadata.shape + @property + def chunks(self) -> ChunkCoords: + if isinstance(self.metadata.chunk_grid, RegularChunkGrid): + return self.metadata.chunk_grid.chunk_shape + else: + raise ValueError( + f"chunk attribute is only available for RegularChunkGrid, this array has a {self.metadata.chunk_grid}" + ) + @property def size(self) -> int: return np.prod(self.metadata.shape).item() @@ -641,6 +650,10 @@ def ndim(self) -> int: def shape(self) -> ChunkCoords: return self._async_array.shape + @property + def chunks(self) -> ChunkCoords: + return self._async_array.chunks + @property def size(self) -> int: return self._async_array.size diff --git a/tests/v3/test_group.py b/tests/v3/test_group.py index 9ce9b07a2..c529e2491 100644 --- a/tests/v3/test_group.py +++ b/tests/v3/test_group.py @@ -79,7 +79,7 @@ def test_group(store: MemoryStore | LocalStore) -> None: assert arr.dtype == data.dtype # TODO: update this once the array api settles down - # assert arr.chunk_shape == (2, 2) + assert arr.chunks == (2, 2) bar2 = foo["bar"] assert dict(bar2.attrs) == {"baz": "qux"}