Skip to content

Ensure that chunks is tuple of ints upon array creation #1470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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