Support a single chunk spec in DataTree.chunk - #11569
Open
fredrikblau wants to merge 1 commit into
Open
Conversation
DataTree.chunk rejected anything that was not a mapping, so
`dt.chunk("auto")` raised TypeError even though its docstring, its
type annotation and Dataset.chunk all accept a single chunk
specification. Broadcast such a value over every dimension in the tree,
as Dataset.chunk does. Sequences of dimension-order sizes remain
rejected: they are deprecated for Dataset.chunk and are ambiguous for a
tree whose groups need not share a dimension ordering.
fredrikblau
force-pushed
the
fix/11315-datatree-chunk-scalar
branch
from
September 7, 2026 09:00
0116038 to
f717651
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
whats-new.rstDataTree.chunkrejects anything that isn't a mapping:That contradicts the method's own docstring and its
chunks: T_ChunksFreqannotation, both of which advertiseint,"auto"or a mapping, and it diverges fromDataset.chunk, which broadcasts a single value across every dimension withdict.fromkeys(self.dims, chunks).This does the same for trees: a non-mapping
chunksis expanded withdict.fromkeys(self._get_all_dims(), chunks)before the existing per-group dispatch, sodt.chunk("auto"),dt.chunk(5)anddt.chunk("20B")work and each group gets chunk sizes for its own dimensions only.The existing guard was commented "don't support deprecated ways of passing chunks", and that part is kept. Tuples and lists still raise
TypeError: they are the formDataset.chunkemits aFutureWarningfor, and for a tree they are ambiguous anyway, since groups need not share an ordering of their dimensions and there is no well-defined sequence to zip against_get_all_dims().Nonealso still raises, matching the behaviourtest_chunkalready asserts. The message is reworded to say what is accepted while keeping theinvalid type for chunks:prefix, so both existingpytest.raises(TypeError, match="invalid type")assertions still pass unchanged.tuple of intwas dropped from the docstring'schunksline, since that form is deliberately not accepted here.I left
T_ChunksFreqalone — it still nominally includestupleandNone, but it is shared withDataset.chunkand narrowing it would be a wider change than this fix warrants.Testing
TestDask::test_chunk_single_specis parametrized over"auto",5and"20B"and compares against the per-groupDataset.chunkresult rather than just asserting no exception, so it pins the actual chunk sizes:tree.chunk(5)gives{'/': {'x': (5, 5)}, '/group1': {'y': (5, 1)}}andtree.chunk("20B")gives{'/': {'x': (2, 2, 2, 2, 2)}, '/group1': {'y': (2, 2, 2)}}— different per group, which is what a broadcast should produce.Reverting
datatree.pyalone fails all three parametrizations with the originalTypeError; with the change they pass.xarray/tests/test_datatree.py: 159 passed, 5 xfailed.xarray/tests/test_dask.py: 219 passed, 2 skipped, 3 xfailed.