From 2c8f2e81e2ac8c49cbdad86367c571cfef9cdba7 Mon Sep 17 00:00:00 2001 From: lkstrp Date: Mon, 28 Oct 2024 15:01:25 +0100 Subject: [PATCH 1/2] add `ValueError` --- xarray/core/dataarray.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index db7824d8c90..c6bc082f5ed 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1444,6 +1444,11 @@ def chunk( "It will raise an error in the future. Instead use a dict with dimension names as keys.", category=DeprecationWarning, ) + if len(chunks) != len(self.dims): + raise ValueError( + f"chunks must have the same number of elements as dimensions. " + f"Expected {len(self.dims)} elements, got {len(chunks)}." + ) chunk_mapping = dict(zip(self.dims, chunks, strict=True)) else: chunk_mapping = either_dict_or_kwargs(chunks, chunks_kwargs, "chunk") From 5d9e178549ad7c0bbb2ceac52f756f59ab4c32e8 Mon Sep 17 00:00:00 2001 From: lkstrp Date: Mon, 28 Oct 2024 20:01:13 +0100 Subject: [PATCH 2/2] add test --- xarray/tests/test_dataarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 78db39c194e..bff5ca8298d 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -897,6 +897,9 @@ def test_chunk(self) -> None: assert blocked.chunks == ((3,), (3, 1)) assert blocked.data.name != first_dask_name + with pytest.raises(ValueError): + blocked.chunk(chunks=(3, 3, 3)) + # name doesn't change when rechunking by same amount # this fails if ReprObject doesn't have __dask_tokenize__ defined assert unblocked.chunk(2).data.name == unblocked.chunk(2).data.name