pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.63k stars 1.09k forks source link

Raise `ValueError` for unmatching chunks length in `DataArray.chunk()` #9689

Closed lkstrp closed 3 weeks ago

lkstrp commented 3 weeks ago

https://github.com/pydata/xarray/pull/9450 introduced strict handling of zip(). In DataArray.chunk(), this leads to a breaking change when passing a tuple/list as chunks where the length doesn't match self.dims. Previously these were just discarded.

https://github.com/pydata/xarray/blob/2c8f2e81e2ac8c49cbdad86367c571cfef9cdba7/xarray/core/dataarray.py#L1452

Since passing a tuple/list as chunks is deprecated anyway, this PR simply adds a ValueError to make the problem clear to the user instead of this ambiguous strict zip error.

Current error:

Traceback (most recent call last):
  File "/Users/../.venv/lib/python3.12/site-packages/xarray/util/deprecation_helpers.py", line 118, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/../.venv/lib/python3.12/site-packages/xarray/core/dataarray.py", line 1447, in chunk
    chunk_mapping = dict(zip(self.dims, chunks, strict=True))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zip() argument 2 is longer than argument 1
welcome[bot] commented 3 weeks ago

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. If you have questions, some answers may be found in our contributing guidelines.

max-sixty commented 3 weeks ago

Adding a test would be great, but maybe this is one time where it's clear enough without one, so will plan to merge either way.

Thank you!

welcome[bot] commented 3 weeks ago

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

max-sixty commented 3 weeks ago

Thanks @lkstrp !