Skip to content

disallow regular chunk grid with 0-length chunks #3711

@d-v-b

Description

@d-v-b

We support creating arrays with 0-sized chunks, but we error when accessing the values via __getitem__. Because the spec defines chunk sizes as non-zero integers, we should error at array creation time here.

>>> zarr.zeros(store={}, shape=(1,5,1), chunks=(1,2,0)).chunks
(1, 2, 0)
>>> zarr.zeros(store={}, shape=(1,5,1), chunks=(1,2,0))[:]    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/d-v-b/dev/data-model-1/.venv/lib/python3.12/site-packages/zarr/core/array.py", line 2868, in __getitem__
    return self.get_orthogonal_selection(pure_selection, fields=fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/d-v-b/dev/data-model-1/.venv/lib/python3.12/site-packages/zarr/core/array.py", line 3338, in get_orthogonal_selection
    indexer = OrthogonalIndexer(selection, self.shape, self.metadata.chunk_grid)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/d-v-b/dev/data-model-1/.venv/lib/python3.12/site-packages/zarr/core/indexing.py", line 923, in __init__
    dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/d-v-b/dev/data-model-1/.venv/lib/python3.12/site-packages/zarr/core/indexing.py", line 424, in __init__
    object.__setattr__(self, "nchunks", ceildiv(dim_len, dim_chunk_len))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/d-v-b/dev/data-model-1/.venv/lib/python3.12/site-packages/zarr/core/common.py", line 94, in ceildiv
    return math.ceil(a / b)
                     ~~^~~
ZeroDivisionError: division by zero

This will be a breaking change for anyone relied on the creation of these arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions