Skip to content

fix: raise a validation error for out-of-bounds integer fill values - #4703

Open
barlowa124 wants to merge 7 commits into
zarr-developers:mainfrom
barlowa124:int-fill-range-4453
Open

barlowa124 wants to merge 7 commits into
zarr-developers:mainfrom
barlowa124:int-fill-range-4453

Conversation

@barlowa124

Copy link
Copy Markdown
Contributor

Supersedes #4492, which GitHub auto-closed when the branch history was rewritten to remove tooling attribution.

Summary

Partial fix for #4453, item 4 first bullet. ArrayV3Metadata.from_dict
with an out-of-range integer fill value leaked NumPy's OverflowError
instead of raising a validation error:

ArrayV3Metadata.from_dict({**ARRAY, "data_type": "int8", "fill_value": 300})
# before: OverflowError: Python integer 300 out of bounds for int8
# after:  TypeError: Invalid type: 300. Integer is out of bounds for int8.

BaseInt.from_json_scalar now bounds-checks the parsed integer against
np.iinfo before casting, so all three accepted JSON forms (int,
integer-valued float, integer string) get the same TypeError the
non-integer path already raises. In-range behavior is unchanged.

For reviewers

The parse path only: cast_scalar still uses NumPy cast semantics, so an
out-of-range fill passed through the create API keeps its current
behavior. The remaining item-4 bullets (byte-array bytes fills, r*
raw-bits dtypes) are not addressed here.

Author attestation

  • I am a human, these are my changes, and I have reviewed and understood every change and can explain why each is correct.

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/user-guide/*.md
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

from_json_scalar cast the parsed integer into the native dtype without a
range check, so fill_value 300 for int8 surfaced a numpy OverflowError
instead of a validation error. Bounds-check against np.iinfo before
casting; int, intish float, and intish string forms are covered.
…shared bounds check

- Raise ValueError (not TypeError) for out-of-range values and drop
  "Invalid type" from the message; format input with {data!r}
- Document the ValueError in the Raises sections of from_json_scalar
  and cast_scalar
- Split the regression test into parametrized in-bounds and
  out-of-bounds tests
- Share the bounds check with cast_scalar so create_array with an
  out-of-range fill_value raises the same error (optional item)
- Changelog: single backticks, cover both read and write paths

This branch has not been deployed

No deployments
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.

1 participant