xarray-contrib / datatree

WIP implementation of a tree-like hierarchical data structure for xarray.
https://xarray-datatree.readthedocs.io
Apache License 2.0
161 stars 43 forks source link

Make illegal path-like variable names when constructing a DataTree from a Dataset #314

Open etienneschalk opened 4 months ago

etienneschalk commented 4 months ago

Technical Note

Regarding Hashable vs str Dataset keys

Note: DataTree keys are Hashable. I only check for slashes in the variable names if they are instance of str. I never encountered a case (yet) where a Dataset keys are not str but Hashable in the broader case. We can imagine corner-cases where keys would be other types of Hashable, eg Path from pathlib

In [2]: from pathlib import Path

In [3]: hash(Path("/"))
Out[3]: -3809984204556177651

The choice I made is (1): only apply the check of slashes in the key if the key is an instance of str. Another choice (2)would be to project the Hashable space onto str space: str(variable_name) (1) seems more conservative than (2) as I do not pretend to be able to get a string representation for any Hashable.