From 1134be0d48ab505bd15ab6e88acb6774fe0fbaa8 Mon Sep 17 00:00:00 2001 From: faymanns Date: Fri, 20 Dec 2024 10:12:48 +0100 Subject: [PATCH 1/2] Warn user when shape or chunks contains non-integer values like floats --- zarr/util.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zarr/util.py b/zarr/util.py index 8a96f92c24..2c9b0f616b 100644 --- a/zarr/util.py +++ b/zarr/util.py @@ -18,6 +18,7 @@ Iterable, cast, ) +import warnings import numpy as np from asciitree import BoxStyle, LeftAligned @@ -88,6 +89,8 @@ def normalize_shape(shape: Union[int, Tuple[int, ...], None]) -> Tuple[int, ...] # normalize shape = cast(Tuple[int, ...], shape) + if not all(isinstance(s, numbers.Integral) for s in shape): + warnings.warn("shape contains non-integer value(s)", UserWarning, stacklevel=2) shape = tuple(int(s) for s in shape) return shape @@ -176,6 +179,9 @@ def normalize_chunks(chunks: Any, shape: Tuple[int, ...], typesize: int) -> Tupl if -1 in chunks or None in chunks: chunks = tuple(s if c == -1 or c is None else int(c) for s, c in zip(shape, chunks)) + if not all(isinstance(c, numbers.Integral) for c in chunks): + warnings.warn("chunks contains non-integer value(s)", UserWarning, stacklevel=2) + chunks = tuple(int(c) for c in chunks) return chunks From 1ae98fd80d9cef0cefbd6b7b5b5340f05a2d016a Mon Sep 17 00:00:00 2001 From: faymanns Date: Fri, 20 Dec 2024 11:19:13 +0100 Subject: [PATCH 2/2] Test for non-integer warnings --- zarr/tests/test_creation.py | 9 ++++++++- zarr/tests/test_util.py | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/zarr/tests/test_creation.py b/zarr/tests/test_creation.py index 8e586abfff..3778141356 100644 --- a/zarr/tests/test_creation.py +++ b/zarr/tests/test_creation.py @@ -2,6 +2,7 @@ import os.path import shutil import warnings +import numbers import numpy as np import pytest @@ -762,7 +763,13 @@ def test_create_with_storage_transformers(at_root): ) def test_shape_chunk_ints(init_shape, init_chunks, shape, chunks): g = open_group() - array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8) + if not isinstance(init_shape[0], numbers.Integral) or not isinstance( + init_chunks[0], numbers.Integral + ): + with pytest.warns(UserWarning): + array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8) + else: + array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8) assert all( isinstance(s, int) for s in array.shape diff --git a/zarr/tests/test_util.py b/zarr/tests/test_util.py index d908c7b2d7..2b71566300 100644 --- a/zarr/tests/test_util.py +++ b/zarr/tests/test_util.py @@ -44,7 +44,8 @@ def test_normalize_shape(): with pytest.raises(TypeError): normalize_shape(None) with pytest.raises(ValueError): - normalize_shape("foo") + with pytest.warns(UserWarning): + normalize_shape("foo") def test_normalize_chunks():