Skip to content

Commit e360200

Browse files
committed
Address review on #4492: ValueError for out-of-range, 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
1 parent 5382837 commit e360200

3 files changed

Lines changed: 64 additions & 20 deletions

File tree

‎changes/4492.bugfix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Raise a validation error for out-of-bounds integer fill values instead of leaking NumPy's ``OverflowError``.
1+
Raise a `ValueError` for out-of-bounds integer fill values instead of leaking NumPy's `OverflowError`, both when reading stored metadata and when casting values at array creation.

‎src/zarr/core/dtype/npy/int.py‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ def _cast_scalar_unchecked(self, data: IntLike) -> Scalar:
143143

144144
return self.to_native_dtype().type(data) # type: ignore[return-value]
145145

146+
def _check_int_bounds(self, data: object, value: int) -> None:
147+
"""
148+
Check that an integer value fits in this data type's range.
149+
150+
Parameters
151+
----------
152+
data : object
153+
The original input, used in the error message.
154+
value : int
155+
The integer value to bounds-check.
156+
157+
Raises
158+
------
159+
ValueError
160+
If ``value`` is outside the range of the native dtype.
161+
"""
162+
info = np.iinfo(self.to_native_dtype())
163+
if value < info.min or value > info.max:
164+
raise ValueError(f"{data!r} is out of bounds for {self.to_native_dtype()}.")
165+
146166
def cast_scalar(self, data: object) -> Scalar:
147167
"""
148168
Attempt to cast a given object to a NumPy integer scalar.
@@ -161,9 +181,12 @@ def cast_scalar(self, data: object) -> Scalar:
161181
------
162182
TypeError
163183
If the data cannot be converted to a NumPy integer scalar.
184+
ValueError
185+
If the data is an integer outside the range of the dtype.
164186
"""
165187

166188
if self._check_scalar(data):
189+
self._check_int_bounds(data, int(data))
167190
return self._cast_scalar_unchecked(data)
168191
msg = (
169192
f"Cannot convert object {data!r} with type {type(data)} to a scalar compatible with the "
@@ -202,17 +225,15 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> Scalar:
202225
------
203226
TypeError
204227
If the input is not a valid integer type.
228+
ValueError
229+
If the input is an integer outside the range of the dtype.
205230
"""
206231
if check_json_int(data) or check_json_intish_float(data) or check_json_intish_str(data):
207232
value = int(data)
208233
else:
209234
raise TypeError(f"Invalid type: {data}. Expected an integer.")
210235

211-
info = np.iinfo(self.to_native_dtype())
212-
if value < info.min or value > info.max:
213-
raise TypeError(
214-
f"Invalid type: {data}. Integer is out of bounds for {self.to_native_dtype()}."
215-
)
236+
self._check_int_bounds(data, value)
216237
return self._cast_scalar_unchecked(value)
217238

218239
def to_json_scalar(self, data: object, *, zarr_format: ZarrFormat) -> int:

‎tests/test_dtype/test_npy/test_int.py‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,31 +340,54 @@ def test_string_integer_from_json_scalar() -> None:
340340
assert result == np.int32(123)
341341

342342

343-
def test_out_of_bounds_integer_from_json_scalar() -> None:
344-
"""An integer outside the dtype's range raises a validation error, not
345-
numpy's OverflowError. Regression test for
346-
https://github.com/zarr-developers/zarr-python/issues/4453 item 4."""
347-
for dtype_instance, bad in (
343+
@pytest.mark.parametrize(
344+
("dtype_instance", "value"),
345+
[
348346
(Int8(), 300),
349347
(Int8(), -129),
350348
(Int8(), "300"),
351349
(Int8(), 300.0),
352350
(UInt8(), -1),
353351
(UInt64(), 2**64),
354352
(Int64(), -(2**63) - 1),
355-
):
356-
with pytest.raises(TypeError, match="out of bounds"):
357-
dtype_instance.from_json_scalar(bad, zarr_format=3)
358-
with pytest.raises(TypeError, match="out of bounds"):
359-
dtype_instance.from_json_scalar(bad, zarr_format=2)
353+
],
354+
)
355+
def test_out_of_bounds_integer_from_json_scalar(
356+
dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: object
357+
) -> None:
358+
"""An integer outside the dtype's range raises a validation error, not
359+
numpy's OverflowError. Regression test for
360+
https://github.com/zarr-developers/zarr-python/issues/4453 item 4."""
361+
for zarr_format in (2, 3):
362+
with pytest.raises(ValueError, match="out of bounds"):
363+
dtype_instance.from_json_scalar(value, zarr_format=zarr_format)
364+
360365

361-
for dtype_instance, good in (
366+
@pytest.mark.parametrize(
367+
("dtype_instance", "value"),
368+
[
362369
(Int8(), 127),
363370
(Int8(), -128),
364371
(UInt8(), 255),
365372
(UInt64(), 2**64 - 1),
366373
(Int64(), -(2**63)),
367-
):
374+
],
375+
)
376+
def test_in_bounds_integer_from_json_scalar(
377+
dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: int
378+
) -> None:
379+
"""In-bounds integers round-trip for both zarr formats."""
380+
for zarr_format in (2, 3):
368381
assert dtype_instance.from_json_scalar(
369-
good, zarr_format=3
370-
) == dtype_instance.to_native_dtype().type(good)
382+
value, zarr_format=zarr_format
383+
) == dtype_instance.to_native_dtype().type(value)
384+
385+
386+
def test_out_of_bounds_integer_cast_scalar() -> None:
387+
"""cast_scalar raises ValueError for out-of-bounds integers, so
388+
create_array(dtype="i1", fill_value=300) fails the same way reading
389+
stored metadata does."""
390+
with pytest.raises(ValueError, match="out of bounds"):
391+
Int8().cast_scalar(300)
392+
with pytest.raises(ValueError, match="out of bounds"):
393+
UInt64().cast_scalar(2**64)

0 commit comments

Comments
 (0)