Skip to content

Commit 6ed4d78

Browse files
authored
Ensure that chunks is tuple of ints upon array creation (#1470)
* Add failing test for creating group with float chunks * Fix flake8 errors * Cast chunks to tuple[int, ...] before returning * Use decorator to cast to int tuple * Fix mypy type issues * Fix black formatting * Add docstring to _as_int_tuple * Document changes in docs/release.rst * Revert to casting to tuple of ints inside normalize_chunks After discussion in #1470, this was selected as the best option
1 parent 6cb3cf1 commit 6ed4d78

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Maintenance
5050
* Style the codebase with ``ruff`` and ``black``.
5151
By :user:`Davis Bennett` <d-v-b> :issue:`1459`
5252

53+
* Ensure that chunks is tuple of ints upon array creation.
54+
By :user:`Philipp Hanslovsky` <hanslovsky> :issue:`1461`
55+
5356
.. _release_2.15.0:
5457

5558
2.15.0

zarr/tests/test_creation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,27 @@ def test_create_with_storage_transformers(at_root):
757757
z = create(1000000000, chunks=True, storage_transformers=[transformer], **kwargs)
758758
assert isinstance(z.chunk_store, DummyStorageTransfomer)
759759
assert z.chunk_store.test_value == DummyStorageTransfomer.TEST_CONSTANT
760+
761+
762+
@pytest.mark.parametrize(
763+
("init_shape", "init_chunks", "shape", "chunks"),
764+
(
765+
((1,), (1,), (1,), (1,)),
766+
((1.0,), (1.0,), (1,), (1,)),
767+
((1.0,), False, (1,), (1,)),
768+
((1.0,), True, (1,), (1,)),
769+
((1.0,), None, (1,), (1,)),
770+
),
771+
)
772+
def test_shape_chunk_ints(init_shape, init_chunks, shape, chunks):
773+
g = open_group()
774+
array = g.create_dataset("ds", shape=init_shape, chunks=init_chunks, dtype=np.uint8)
775+
776+
assert all(
777+
isinstance(s, int) for s in array.shape
778+
), f"Expected shape to be all ints but found {array.shape=}."
779+
assert all(
780+
isinstance(c, int) for c in array.chunks
781+
), f"Expected chunks to be all ints but found {array.chunks=}."
782+
assert array.shape == shape, f"Expected {shape=} but found {array.shape=}."
783+
assert array.chunks == chunks, f"Expected {chunks=} but found {array.chunks=}."

zarr/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def normalize_chunks(chunks: Any, shape: Tuple[int, ...], typesize: int) -> Tupl
175175
if -1 in chunks or None in chunks:
176176
chunks = tuple(s if c == -1 or c is None else int(c) for s, c in zip(shape, chunks))
177177

178-
return tuple(chunks)
178+
chunks = tuple(int(c) for c in chunks)
179+
return chunks
179180

180181

181182
def normalize_dtype(dtype: Union[str, np.dtype], object_codec) -> Tuple[np.dtype, Any]:

0 commit comments

Comments
 (0)