Skip to content

Commit

Permalink
correct array.nbytes, and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-v-b committed Dec 19, 2024
1 parent 5bf7bcf commit 9e8f351
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,10 @@ def _iter_chunk_regions(
@property
def nbytes(self) -> int:
"""
The number of bytes that can be stored in this array.
The number of bytes that can be stored in the chunks of this array.
"""
return self.nchunks * self.dtype.itemsize
# TODO: how can this be meaningful for variable-length types?
return int(np.prod(self.shape) * self.dtype.itemsize)

async def _get_selection(
self,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,21 @@ async def test_special_complex_fill_values_roundtrip(fill_value: Any, expected:
assert content is not None
actual = json.loads(content.to_bytes())
assert actual["fill_value"] == expected


@pytest.mark.parametrize("shape", [(1,), (2, 3), (4, 5, 6)])
@pytest.mark.parametrize("dtype", ["uint8", "float32"])
@pytest.mark.parametrize("array_type", ["async", "sync"])
async def test_nbytes(
shape: tuple[int, ...], dtype: str, array_type: Literal["async", "sync"]
) -> None:
"""
Test that the ``nbytes`` attribute of an Array or AsyncArray correctly reports the capacity of
the chunks of that array.
"""
store = MemoryStore()
arr = Array.create(store=store, shape=shape, dtype=dtype, fill_value=0)
if array_type == "async":
assert arr._async_array.nbytes == np.prod(arr.shape) * arr.dtype.itemsize
else:
assert arr.nbytes == np.prod(arr.shape) * arr.dtype.itemsize

0 comments on commit 9e8f351

Please sign in to comment.