Skip to content

Commit 9e8f351

Browse files
committed
correct array.nbytes, and add tests
1 parent 5bf7bcf commit 9e8f351

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/zarr/core/array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,10 @@ def _iter_chunk_regions(
977977
@property
978978
def nbytes(self) -> int:
979979
"""
980-
The number of bytes that can be stored in this array.
980+
The number of bytes that can be stored in the chunks of this array.
981981
"""
982-
return self.nchunks * self.dtype.itemsize
982+
# TODO: how can this be meaningful for variable-length types?
983+
return int(np.prod(self.shape) * self.dtype.itemsize)
983984

984985
async def _get_selection(
985986
self,

tests/test_array.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,21 @@ async def test_special_complex_fill_values_roundtrip(fill_value: Any, expected:
776776
assert content is not None
777777
actual = json.loads(content.to_bytes())
778778
assert actual["fill_value"] == expected
779+
780+
781+
@pytest.mark.parametrize("shape", [(1,), (2, 3), (4, 5, 6)])
782+
@pytest.mark.parametrize("dtype", ["uint8", "float32"])
783+
@pytest.mark.parametrize("array_type", ["async", "sync"])
784+
async def test_nbytes(
785+
shape: tuple[int, ...], dtype: str, array_type: Literal["async", "sync"]
786+
) -> None:
787+
"""
788+
Test that the ``nbytes`` attribute of an Array or AsyncArray correctly reports the capacity of
789+
the chunks of that array.
790+
"""
791+
store = MemoryStore()
792+
arr = Array.create(store=store, shape=shape, dtype=dtype, fill_value=0)
793+
if array_type == "async":
794+
assert arr._async_array.nbytes == np.prod(arr.shape) * arr.dtype.itemsize
795+
else:
796+
assert arr.nbytes == np.prod(arr.shape) * arr.dtype.itemsize

0 commit comments

Comments
 (0)