Skip to content

Conversation

@eshort0401
Copy link

@eshort0401 eshort0401 commented Dec 14, 2025

Addresses #9490 and #9978 by forbidding the / character in DataTree child names.

  1. DataTree node names are already checked using _validate_name from the treenode module. I therefore call this function inside the _check_children member function of TreeNode. This check ensures invalid children arguments are caught early in the DataTree constructor, and when the DataTree.children attribute is reset explicitly (see Set child nodes via DataTree.children #9441). I have also reworded the _validate_name error message slightly for consistency with _check_for_slashes_in_names (defined in the datatree module) which checks variable names. To illustrate, code like
    import xarray as xr
    ds = xr.Dataset({"a": xr.DataArray([1, 2, 3])})
    dt = xr.DataTree(dataset=ds, children={"x/y": xr.DataTree()})
    and
    import xarray as xr
    ds = xr.Dataset({"a": xr.DataArray([1, 2, 3])})
    dt = xr.DataTree(dataset=ds, children={"x": xr.DataTree()})
    dt.children = {"x/y": xr.DataTree()}
    will now both produce
    ValueError: Node name 'x/y' contains the '/' character. Nodes cannot have names containing '/' characters, as this would make path-like access to nodes ambiguous.
    
  2. I have also added a call to _check_children on the input children mapping given to the DataTree constructor, before it is assigned and the setter function is called. This additional check ensures that if the user passes a bad children, a helpful error message is given before the shallow copy {name: child.copy() for name, child in children.items()} fails.
  3. I have added a couple of lines to the DataTree.__init__ docstring to communicate that / characters are forbidden in both the name argument, and the keys of the children mapping argument.

Additional Notes

This PR follows after discussion with @TomNicholas in #11001. See also #9441 and #9477.

@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Dec 14, 2025
@eshort0401
Copy link
Author

I've just realized this PR is very similar to @TomNicholas's PR #9492 from 2024, sorry! Happy to update authorship in the whats-new.rst if appropriate. Not sure why #9492 wasn't merged as a temporary fix for #9485.

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.

Using a forward slash in a node names of a DataTree causes a RecursionError Forbid path-like child names

1 participant