Skip to content

Fix misleading DataTree constructor error message (#11514) - #11564

Open
IMGillusion wants to merge 2 commits into
pydata:mainfrom
IMGillusion:fix-11514-datatr-error-msg
Open

Fix misleading DataTree constructor error message (#11514)#11564
IMGillusion wants to merge 2 commits into
pydata:mainfrom
IMGillusion:fix-11514-datatr-error-msg

Conversation

@IMGillusion

Copy link
Copy Markdown

Fixes #11514.

What happened

xr.DataTree(dict()) raises

TypeError: data object is not an xarray.Dataset, dict, or None: {}

but DataTree.__init__ only accepts an xarray.Dataset, xarray.Coordinates, or None for its dataset argument — _to_new_dataset has branches for exactly those three. The dict in the message is a leftover from an earlier API iteration and is misleading (it advertises a type the constructor does not accept).

Change

Update the TypeError message in _to_new_dataset (xarray/core/datatree.py) to name xarray.Coordinates instead of dict. This is a copy-only fix — it does not change constructor behaviour (accepting dict would be a separate, larger API decision).

Test

Added a regression assertion to TestTreeCreation.test_data_arg: DataTree(dataset=dict()) must raise a TypeError whose message references xarray.Coordinates.

Validation

  • reproduces on pristine code (message still said dict), fixed version passes
  • full test_datatree.py passes (151 passed, 5 skipped, 5 xfailed), no regressions
  • DataTree(xr.Coordinates(...)) still constructs as expected

The `DataTree` constructor only accepts an `xarray.Dataset`,
`xarray.Coordinates`, or `None` as its `dataset` argument, but the
TypeError raised for any other input incorrectly listed `dict` as a
valid type (a leftover from an earlier API iteration). Update the
message to name `xarray.Coordinates` so it is not misleading.

Add a regression test in `TestTreeCreation.test_data_arg`.
@github-actions github-actions Bot added the topic-DataTree Related to the implementation of a DataTree class label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-DataTree Related to the implementation of a DataTree class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataTree constructor error message says dict even though dict is not accepted

1 participant