Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct array.nbytes, and add tests #2576

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding a note or warning that this only gives the correct answer for fixed-length types? (if I'm interpreting your TODO correctly?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also just fix the implementation ... what kind of behavior would people expect when calling nbytes on an array with variable length types? maybe raise an exception?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess an exception, or return None? Is there a precendent elsewhere in the codebase for which to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

second question, is there any reliable way to check if a dtype is variable length, other than a big lookup table?

cc @rabernat

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm feeling like this out of scope for this PR; numpy itself reports a (misleading) integer itemsize for variable length dtypes already, so we can lean on that bad precedent until we come up with a proper fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - can you add a quick note to the docstring here though noting that it's incorrect for variable length dtypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a look at the docstrings I added in 254e276

"""
return self.nchunks * self.dtype.itemsize
# TODO: how can this be meaningful for variable-length types?
return self.size * self.dtype.itemsize

async def _get_selection(
self,
Expand Down Expand Up @@ -1429,7 +1430,7 @@ def _info(
_order=self.order,
_read_only=self.read_only,
_store_type=type(self.store_path.store).__name__,
_count_bytes=self.dtype.itemsize * self.size,
_count_bytes=self.nbytes,
_count_bytes_stored=count_bytes_stored,
_count_chunks_initialized=count_chunks_initialized,
**kwargs,
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
dstansby marked this conversation as resolved.
Show resolved Hide resolved
else:
assert arr.nbytes == np.prod(arr.shape) * arr.dtype.itemsize
Loading