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

correct array.nbytes, and add tests #2576

merged 3 commits into from
Dec 19, 2024

Conversation

d-v-b
Copy link
Contributor

@d-v-b d-v-b commented Dec 19, 2024

AsyncArray.nbytes is very wrong, this PR corrects it (and adds tests)

closes #2575

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/tutorial.rst
  • Changes documented in docs/release.rst
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

Copy link
Contributor

@rabernat rabernat left a comment

Choose a reason for hiding this comment

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

One minor suggestion to be self consistent in our implementation.

We should reuse this function within info (where it is currently correct) rather than duplicating the same logic twice.

src/zarr/core/array.py Outdated Show resolved Hide resolved
@@ -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

tests/test_array.py Show resolved Hide resolved
@d-v-b d-v-b enabled auto-merge (squash) December 19, 2024 16:27
@d-v-b d-v-b merged commit 1cc3917 into main Dec 19, 2024
32 checks passed
@dstansby dstansby deleted the fix/correct-array-nbytes branch December 19, 2024 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array.nbytes logic is wrong
3 participants