Skip to content

Commit

Permalink
implement .chunks on v3 arrays (zarr-developers#1929)
Browse files Browse the repository at this point in the history
* implement .chunks on v3 arrays

* remove noqa: B009

* make mypy happy

* only return chunks for regular chunk grids

---------

Co-authored-by: Davis Bennett <[email protected]>
Co-authored-by: Joseph Hamman <[email protected]>
  • Loading branch information
3 people committed Jun 3, 2024
1 parent 72005d7 commit da9885c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit da9885c

Please sign in to comment.