Skip to content

Commit

Permalink
Ensure that chunks is tuple of ints upon array creation (#1470)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hanslovsky authored Jul 27, 2023
1 parent 6cb3cf1 commit 6ed4d78
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Maintenance
* Style the codebase with ``ruff`` and ``black``.
By :user:`Davis Bennett` <d-v-b> :issue:`1459`

* Ensure that chunks is tuple of ints upon array creation.
By :user:`Philipp Hanslovsky` <hanslovsky> :issue:`1461`

.. _release_2.15.0:

2.15.0
Expand Down
24 changes: 24 additions & 0 deletions zarr/tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,27 @@ def test_create_with_storage_transformers(at_root):
z = create(1000000000, chunks=True, storage_transformers=[transformer], **kwargs)
assert isinstance(z.chunk_store, DummyStorageTransfomer)
assert z.chunk_store.test_value == DummyStorageTransfomer.TEST_CONSTANT


@pytest.mark.parametrize(
("init_shape", "init_chunks", "shape", "chunks"),
(
((1,), (1,), (1,), (1,)),
((1.0,), (1.0,), (1,), (1,)),
((1.0,), False, (1,), (1,)),
((1.0,), True, (1,), (1,)),
((1.0,), None, (1,), (1,)),
),
)
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)

assert all(
isinstance(s, int) for s in array.shape
), f"Expected shape to be all ints but found {array.shape=}."
assert all(
isinstance(c, int) for c in array.chunks
), f"Expected chunks to be all ints but found {array.chunks=}."
assert array.shape == shape, f"Expected {shape=} but found {array.shape=}."
assert array.chunks == chunks, f"Expected {chunks=} but found {array.chunks=}."
3 changes: 2 additions & 1 deletion zarr/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ 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))

return tuple(chunks)
chunks = tuple(int(c) for c in chunks)
return chunks


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

0 comments on commit 6ed4d78

Please sign in to comment.