This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Description
Currently this works
dt["a"] = xr.DataArray(0)
but this fails
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 dt["a"] = 0
File ~/Documents/Work/Code/datatree/datatree/datatree.py:704, in DataTree.__setitem__(self, key, value)
700 elif isinstance(key, str):
701 # TODO should possibly deal with hashables in general?
702 # path-like: a name of a node/variable, or path to a node/variable
703 path = NodePath(key)
--> 704 return self._set_item(path, value, new_nodes_along_path=True)
705 else:
706 raise ValueError("Invalid format for key")
File ~/Documents/Work/Code/datatree/datatree/treenode.py:444, in TreeNode._set_item(self, path, item, new_nodes_along_path, allow_overwrite)
442 raise KeyError(f"Already a node object at path {path}")
443 else:
--> 444 current_node._set(name, item)
File ~/Documents/Work/Code/datatree/datatree/datatree.py:684, in DataTree._set(self, key, val)
682 self.update({key: val})
683 else:
--> 684 raise TypeError(f"Type {type(val)} cannot be assigned to a DataTree")
TypeError: Type <class 'int'> cannot be assigned to a DataTree
The latter syntax is convenient and intuitive, so we should relax this to accept any type, and raise an error if the DataArray constructor can't parse it.
However it's not totally trivial to implement because it breaks some assumptions in the code that walks the tree.