Skip to content

Commit 9ac82d1

Browse files
authored
Merge branch 'main' into default-compressor
2 parents 3933c05 + 1cc3917 commit 9ac82d1

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/zarr/core/array.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,17 @@ def _iter_chunk_regions(
999999
@property
10001000
def nbytes(self) -> int:
10011001
"""
1002-
The number of bytes that can be stored in this array.
1002+
The total number of bytes that can be stored in the chunks of this array.
1003+
1004+
Notes
1005+
-----
1006+
This value is calculated by multiplying the number of elements in the array and the size
1007+
of each element, the latter of which is determined by the dtype of the array.
1008+
For this reason, ``nbytes`` will likely be inaccurate for arrays with variable-length
1009+
dtypes. It is not possible to determine the size of an array with variable-length elements
1010+
from the shape and dtype alone.
10031011
"""
1004-
return self.nchunks * self.dtype.itemsize
1012+
return self.size * self.dtype.itemsize
10051013

10061014
async def _get_selection(
10071015
self,
@@ -1451,7 +1459,7 @@ def _info(
14511459
_order=self.order,
14521460
_read_only=self.read_only,
14531461
_store_type=type(self.store_path.store).__name__,
1454-
_count_bytes=self.dtype.itemsize * self.size,
1462+
_count_bytes=self.nbytes,
14551463
_count_bytes_stored=count_bytes_stored,
14561464
_count_chunks_initialized=count_chunks_initialized,
14571465
**kwargs,
@@ -1792,7 +1800,15 @@ def _iter_chunk_coords(
17921800
@property
17931801
def nbytes(self) -> int:
17941802
"""
1795-
The number of bytes that can be stored in this array.
1803+
The total number of bytes that can be stored in the chunks of this array.
1804+
1805+
Notes
1806+
-----
1807+
This value is calculated by multiplying the number of elements in the array and the size
1808+
of each element, the latter of which is determined by the dtype of the array.
1809+
For this reason, ``nbytes`` will likely be inaccurate for arrays with variable-length
1810+
dtypes. It is not possible to determine the size of an array with variable-length elements
1811+
from the shape and dtype alone.
17961812
"""
17971813
return self._async_array.nbytes
17981814

tests/test_array.py

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

0 commit comments

Comments
 (0)