Skip to content

Commit c969f5c

Browse files
authored
Warn the user when shape or chunks contains float values (#2579)
* Warn user when shape or chunks contains non-integer values like floats * Test for non-integer warnings
1 parent 2ab280a commit c969f5c

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

zarr/tests/test_creation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import shutil
44
import warnings
5+
import numbers
56

67
import numpy as np
78
import pytest
@@ -762,7 +763,13 @@ def test_create_with_storage_transformers(at_root):
762763
)
763764
def test_shape_chunk_ints(init_shape, init_chunks, shape, chunks):
764765
g = open_group()
765-
array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8)
766+
if not isinstance(init_shape[0], numbers.Integral) or not isinstance(
767+
init_chunks[0], numbers.Integral
768+
):
769+
with pytest.warns(UserWarning):
770+
array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8)
771+
else:
772+
array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8)
766773

767774
assert all(
768775
isinstance(s, int) for s in array.shape

zarr/tests/test_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def test_normalize_shape():
4444
with pytest.raises(TypeError):
4545
normalize_shape(None)
4646
with pytest.raises(ValueError):
47-
normalize_shape("foo")
47+
with pytest.warns(UserWarning):
48+
normalize_shape("foo")
4849

4950

5051
def test_normalize_chunks():

zarr/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Iterable,
1919
cast,
2020
)
21+
import warnings
2122

2223
import numpy as np
2324
from asciitree import BoxStyle, LeftAligned
@@ -88,6 +89,8 @@ def normalize_shape(shape: Union[int, Tuple[int, ...], None]) -> Tuple[int, ...]
8889

8990
# normalize
9091
shape = cast(Tuple[int, ...], shape)
92+
if not all(isinstance(s, numbers.Integral) for s in shape):
93+
warnings.warn("shape contains non-integer value(s)", UserWarning, stacklevel=2)
9194
shape = tuple(int(s) for s in shape)
9295
return shape
9396

@@ -176,6 +179,9 @@ def normalize_chunks(chunks: Any, shape: Tuple[int, ...], typesize: int) -> Tupl
176179
if -1 in chunks or None in chunks:
177180
chunks = tuple(s if c == -1 or c is None else int(c) for s, c in zip(shape, chunks))
178181

182+
if not all(isinstance(c, numbers.Integral) for c in chunks):
183+
warnings.warn("chunks contains non-integer value(s)", UserWarning, stacklevel=2)
184+
179185
chunks = tuple(int(c) for c in chunks)
180186
return chunks
181187

0 commit comments

Comments
 (0)