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

Commit 4eb70a6

Browse files
Fix name permanence behaviour in update method (#178)
* test for name permanence in update * ensure node is copied on update * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * black * whatsnew Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 19572e2 commit 4eb70a6

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

datatree/datatree.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ def _replace(
577577
datatree. It is up to the caller to ensure that they have the right type
578578
and are not used elsewhere.
579579
"""
580+
# TODO Adding new children inplace using this method will cause bugs.
581+
# You will end up with an inconsistency between the name of the child node and the key the child is stored under.
582+
# Use ._set() instead for now
580583
if inplace:
581584
if variables is not None:
582585
self._variables = variables
@@ -801,7 +804,10 @@ def update(self, other: Dataset | Mapping[str, DataTree | DataArray]) -> None:
801804
new_variables = {}
802805
for k, v in other.items():
803806
if isinstance(v, DataTree):
804-
new_children[k] = v
807+
# avoid named node being stored under inconsistent key
808+
new_child = v.copy()
809+
new_child.name = k
810+
new_children[k] = new_child
805811
elif isinstance(v, (DataArray, Variable)):
806812
# TODO this should also accommodate other types that can be coerced into Variables
807813
new_variables[k] = v

datatree/tests/test_datatree.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ def test_update_new_named_dataarray(self):
213213
expected = da.rename("results")
214214
xrt.assert_equal(folder1["results"], expected)
215215

216+
def test_update_doesnt_alter_child_name(self):
217+
dt = DataTree()
218+
dt.update({"foo": xr.DataArray(0), "a": DataTree(name="b")})
219+
assert "a" in dt.children
220+
child = dt["a"]
221+
assert child.name == "a"
222+
216223

217224
class TestCopy:
218225
def test_copy(self, create_test_datatree):

docs/source/whats-new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Breaking changes
3737

3838
- :py:meth:`DataTree.copy` copy method now only copies the subtree, not the parent nodes (:pull:`171`).
3939
By `Tom Nicholas <https://github.com/TomNicholas>`_.
40-
- Grafting a subtree onto another tree now leaves name of original subtree object unchanged (:issue:`116`, :pull:`172`).
40+
- Grafting a subtree onto another tree now leaves name of original subtree object unchanged (:issue:`116`, :pull:`172`, :pull:`178`).
4141
By `Tom Nicholas <https://github.com/TomNicholas>`_.
4242

4343
Deprecations

0 commit comments

Comments
 (0)