From fa50b33a23ae93643b8965909d6013ec2790458d Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Thu, 8 Oct 2026 15:30:41 -0400 Subject: [PATCH 1/7] fix: raise a validation error for out-of-bounds integer fill values 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. --- src/zarr/core/dtype/npy/int.py | 24 +++++++++++++++--------- tests/test_dtype/test_npy/test_int.py | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/zarr/core/dtype/npy/int.py b/src/zarr/core/dtype/npy/int.py index dfb63afc50..c023b89394 100644 --- a/src/zarr/core/dtype/npy/int.py +++ b/src/zarr/core/dtype/npy/int.py @@ -238,15 +238,21 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> Scalar: TypeError If the input is not a valid integer type. """ - if check_json_int(data): - return self._cast_scalar_unchecked(data) - if check_json_intish_float(data): - return self._cast_scalar_unchecked(int(data)) - - if check_json_intish_str(data): - return self._cast_scalar_unchecked(int(data)) - - raise TypeError(f"Invalid type: {data}. Expected an integer.") + if ( + check_json_int(data) + or check_json_intish_float(data) + or check_json_intish_str(data) + ): + value = int(data) + else: + raise TypeError(f"Invalid type: {data}. Expected an integer.") + + info = np.iinfo(self.to_native_dtype()) + if value < info.min or value > info.max: + raise TypeError( + f"Invalid type: {data}. Integer is out of bounds for {self.to_native_dtype()}." + ) + return self._cast_scalar_unchecked(value) def to_json_scalar(self, data: object, *, zarr_format: ZarrFormat) -> int: """ diff --git a/tests/test_dtype/test_npy/test_int.py b/tests/test_dtype/test_npy/test_int.py index 91fd699b4d..3ffb049ed9 100644 --- a/tests/test_dtype/test_npy/test_int.py +++ b/tests/test_dtype/test_npy/test_int.py @@ -365,3 +365,30 @@ def test_match_dtype_not_a_numpy_dtype(not_a_dtype: Any) -> None: """ with pytest.raises(ValueError, match="No Zarr data type found"): data_type_registry.match_dtype(not_a_dtype) + +def test_out_of_bounds_integer_from_json_scalar() -> None: + """An integer outside the dtype's range raises a validation error, not + numpy's OverflowError. Regression test for + https://github.com/zarr-developers/zarr-python/issues/4453 item 4.""" + for dtype_instance, bad in ( + (Int8(), 300), + (Int8(), -129), + (Int8(), "300"), + (Int8(), 300.0), + (UInt8(), -1), + (UInt64(), 2**64), + (Int64(), -(2**63) - 1), + ): + with pytest.raises(TypeError, match="out of bounds"): + dtype_instance.from_json_scalar(bad, zarr_format=3) + with pytest.raises(TypeError, match="out of bounds"): + dtype_instance.from_json_scalar(bad, zarr_format=2) + + for dtype_instance, good in ( + (Int8(), 127), + (Int8(), -128), + (UInt8(), 255), + (UInt64(), 2**64 - 1), + (Int64(), -(2**63)), + ): + assert dtype_instance.from_json_scalar(good, zarr_format=3) == dtype_instance.to_native_dtype().type(good) From 92d6026f7bef9109ee52c1d12dc75ec3edec717c Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Thu, 8 Oct 2026 15:31:42 -0400 Subject: [PATCH 2/7] docs: changelog entry for #4492 --- changes/4492.bugfix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/4492.bugfix.md diff --git a/changes/4492.bugfix.md b/changes/4492.bugfix.md new file mode 100644 index 0000000000..222b277c8c --- /dev/null +++ b/changes/4492.bugfix.md @@ -0,0 +1 @@ +Raise a validation error for out-of-bounds integer fill values instead of leaking NumPy's ``OverflowError``. From 39c4c1cda0147ea0eecd2390138a0987ae09baf7 Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Thu, 8 Oct 2026 16:18:37 -0400 Subject: [PATCH 3/7] style: collapse from_json_scalar condition per ruff format --- src/zarr/core/dtype/npy/int.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/zarr/core/dtype/npy/int.py b/src/zarr/core/dtype/npy/int.py index c023b89394..f5d9d05956 100644 --- a/src/zarr/core/dtype/npy/int.py +++ b/src/zarr/core/dtype/npy/int.py @@ -238,11 +238,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> Scalar: TypeError If the input is not a valid integer type. """ - if ( - check_json_int(data) - or check_json_intish_float(data) - or check_json_intish_str(data) - ): + if check_json_int(data) or check_json_intish_float(data) or check_json_intish_str(data): value = int(data) else: raise TypeError(f"Invalid type: {data}. Expected an integer.") From 4f4a71f66c2c18d76b63c9d2ee652fc287b4445e Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Thu, 8 Oct 2026 16:33:16 -0400 Subject: [PATCH 4/7] style: ruff format the new test --- tests/test_dtype/test_npy/test_int.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_dtype/test_npy/test_int.py b/tests/test_dtype/test_npy/test_int.py index 3ffb049ed9..bc86ad39ac 100644 --- a/tests/test_dtype/test_npy/test_int.py +++ b/tests/test_dtype/test_npy/test_int.py @@ -391,4 +391,6 @@ def test_out_of_bounds_integer_from_json_scalar() -> None: (UInt64(), 2**64 - 1), (Int64(), -(2**63)), ): - assert dtype_instance.from_json_scalar(good, zarr_format=3) == dtype_instance.to_native_dtype().type(good) + assert dtype_instance.from_json_scalar( + good, zarr_format=3 + ) == dtype_instance.to_native_dtype().type(good) From a086c3d2121ceb3f24b3b624d398a6b90bd5f2bf Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Fri, 9 Oct 2026 15:07:03 -0400 Subject: [PATCH 5/7] 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 --- changes/4492.bugfix.md | 2 +- src/zarr/core/dtype/npy/int.py | 31 +++++++++++++--- tests/test_dtype/test_npy/test_int.py | 52 +++++++++++++++++++-------- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/changes/4492.bugfix.md b/changes/4492.bugfix.md index 222b277c8c..83500c2deb 100644 --- a/changes/4492.bugfix.md +++ b/changes/4492.bugfix.md @@ -1 +1 @@ -Raise a validation error for out-of-bounds integer fill values instead of leaking NumPy's ``OverflowError``. +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. diff --git a/src/zarr/core/dtype/npy/int.py b/src/zarr/core/dtype/npy/int.py index f5d9d05956..abb0482953 100644 --- a/src/zarr/core/dtype/npy/int.py +++ b/src/zarr/core/dtype/npy/int.py @@ -178,6 +178,26 @@ def _cast_scalar_unchecked(self, data: IntLike) -> Scalar: return self.to_native_dtype().type(data) # type: ignore[return-value] + def _check_int_bounds(self, data: object, value: int) -> None: + """ + Check that an integer value fits in this data type's range. + + Parameters + ---------- + data : object + The original input, used in the error message. + value : int + The integer value to bounds-check. + + Raises + ------ + ValueError + If ``value`` is outside the range of the native dtype. + """ + info = np.iinfo(self.to_native_dtype()) + if value < info.min or value > info.max: + raise ValueError(f"{data!r} is out of bounds for {self.to_native_dtype()}.") + def cast_scalar(self, data: object) -> Scalar: """ Attempt to cast a given object to a NumPy integer scalar. @@ -196,9 +216,12 @@ def cast_scalar(self, data: object) -> Scalar: ------ TypeError If the data cannot be converted to a NumPy integer scalar. + ValueError + If the data is an integer outside the range of the dtype. """ if self._check_scalar(data): + self._check_int_bounds(data, int(data)) return self._cast_scalar_unchecked(data) msg = ( f"Cannot convert object {data!r} with type {type(data)} to a scalar compatible with the " @@ -237,17 +260,15 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> Scalar: ------ TypeError If the input is not a valid integer type. + ValueError + If the input is an integer outside the range of the dtype. """ if check_json_int(data) or check_json_intish_float(data) or check_json_intish_str(data): value = int(data) else: raise TypeError(f"Invalid type: {data}. Expected an integer.") - info = np.iinfo(self.to_native_dtype()) - if value < info.min or value > info.max: - raise TypeError( - f"Invalid type: {data}. Integer is out of bounds for {self.to_native_dtype()}." - ) + self._check_int_bounds(data, value) return self._cast_scalar_unchecked(value) def to_json_scalar(self, data: object, *, zarr_format: ZarrFormat) -> int: diff --git a/tests/test_dtype/test_npy/test_int.py b/tests/test_dtype/test_npy/test_int.py index bc86ad39ac..88a1fdae65 100644 --- a/tests/test_dtype/test_npy/test_int.py +++ b/tests/test_dtype/test_npy/test_int.py @@ -366,11 +366,10 @@ def test_match_dtype_not_a_numpy_dtype(not_a_dtype: Any) -> None: with pytest.raises(ValueError, match="No Zarr data type found"): data_type_registry.match_dtype(not_a_dtype) -def test_out_of_bounds_integer_from_json_scalar() -> None: - """An integer outside the dtype's range raises a validation error, not - numpy's OverflowError. Regression test for - https://github.com/zarr-developers/zarr-python/issues/4453 item 4.""" - for dtype_instance, bad in ( + +@pytest.mark.parametrize( + ("dtype_instance", "value"), + [ (Int8(), 300), (Int8(), -129), (Int8(), "300"), @@ -378,19 +377,44 @@ def test_out_of_bounds_integer_from_json_scalar() -> None: (UInt8(), -1), (UInt64(), 2**64), (Int64(), -(2**63) - 1), - ): - with pytest.raises(TypeError, match="out of bounds"): - dtype_instance.from_json_scalar(bad, zarr_format=3) - with pytest.raises(TypeError, match="out of bounds"): - dtype_instance.from_json_scalar(bad, zarr_format=2) + ], +) +def test_out_of_bounds_integer_from_json_scalar( + dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: object +) -> None: + """An integer outside the dtype's range raises a validation error, not + numpy's OverflowError. Regression test for + https://github.com/zarr-developers/zarr-python/issues/4453 item 4.""" + for zarr_format in (2, 3): + with pytest.raises(ValueError, match="out of bounds"): + dtype_instance.from_json_scalar(value, zarr_format=zarr_format) + - for dtype_instance, good in ( +@pytest.mark.parametrize( + ("dtype_instance", "value"), + [ (Int8(), 127), (Int8(), -128), (UInt8(), 255), (UInt64(), 2**64 - 1), (Int64(), -(2**63)), - ): + ], +) +def test_in_bounds_integer_from_json_scalar( + dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: int +) -> None: + """In-bounds integers round-trip for both zarr formats.""" + for zarr_format in (2, 3): assert dtype_instance.from_json_scalar( - good, zarr_format=3 - ) == dtype_instance.to_native_dtype().type(good) + value, zarr_format=zarr_format + ) == dtype_instance.to_native_dtype().type(value) + + +def test_out_of_bounds_integer_cast_scalar() -> None: + """cast_scalar raises ValueError for out-of-bounds integers, so + create_array(dtype="i1", fill_value=300) fails the same way reading + stored metadata does.""" + with pytest.raises(ValueError, match="out of bounds"): + Int8().cast_scalar(300) + with pytest.raises(ValueError, match="out of bounds"): + UInt64().cast_scalar(2**64) From 080356ea07cd708101df4b0fe776cc9ad3fa3ae7 Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Fri, 9 Oct 2026 16:43:59 -0400 Subject: [PATCH 6/7] test: type out-of-bounds param as JSON for mypy --- tests/test_dtype/test_npy/test_int.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_dtype/test_npy/test_int.py b/tests/test_dtype/test_npy/test_int.py index 88a1fdae65..2ade141832 100644 --- a/tests/test_dtype/test_npy/test_int.py +++ b/tests/test_dtype/test_npy/test_int.py @@ -6,6 +6,7 @@ import pytest from tests.test_dtype.test_wrapper import BaseTestZDType +from zarr.core.common import JSON from zarr.core.dtype import data_type_registry from zarr.core.dtype.npy.int import Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 from zarr.errors import DataTypeValidationError @@ -380,7 +381,7 @@ def test_match_dtype_not_a_numpy_dtype(not_a_dtype: Any) -> None: ], ) def test_out_of_bounds_integer_from_json_scalar( - dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: object + dtype_instance: Int8 | UInt8 | UInt64 | Int64, value: JSON ) -> None: """An integer outside the dtype's range raises a validation error, not numpy's OverflowError. Regression test for From 527e7be7a81170b30c7daada4943fb16df5903f1 Mon Sep 17 00:00:00 2001 From: barlowa124 Date: Fri, 9 Oct 2026 17:45:10 -0400 Subject: [PATCH 7/7] Move JSON import into TYPE_CHECKING block --- tests/test_dtype/test_npy/test_int.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_dtype/test_npy/test_int.py b/tests/test_dtype/test_npy/test_int.py index 2ade141832..296aa475f8 100644 --- a/tests/test_dtype/test_npy/test_int.py +++ b/tests/test_dtype/test_npy/test_int.py @@ -1,16 +1,18 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING, Any import numpy as np import pytest from tests.test_dtype.test_wrapper import BaseTestZDType -from zarr.core.common import JSON from zarr.core.dtype import data_type_registry from zarr.core.dtype.npy.int import Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 from zarr.errors import DataTypeValidationError +if TYPE_CHECKING: + from zarr.core.common import JSON + class TestInt8(BaseTestZDType): test_cls = Int8