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

Commit f2fbf44

Browse files
authoredDec 31, 2022
Name permanence via shallow copy (#172)
* added descendants property * tests for descendants, lineage, ancestors, subtree * added descendants to API docs * whatsnew * rerun tests * rewrote copy method * remove outdated mypy ignore error * changed tests to reflect new expected behaviour * shallow copy on insertion * update test for checking isomorphism from root * whatsnew
1 parent 667ea97 commit f2fbf44

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed
 

‎datatree/datatree.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,8 @@ def _set(self, key: str, val: DataTree | CoercibleValue) -> None:
754754
Counterpart to the public .get method, and also only works on the immediate node, not other nodes in the tree.
755755
"""
756756
if isinstance(val, DataTree):
757-
# TODO shallow copy here so as not to alter name of node in original tree?
758-
# new_node = copy.copy(val, deep=False)
759-
new_node = val
757+
# create and assign a shallow copy here so as not to alter original name of node in grafted tree
758+
new_node = val.copy(deep=False)
760759
new_node.name = key
761760
new_node.parent = self
762761
else:

‎datatree/tests/test_datatree.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ def test_child_gets_named_on_attach(self):
6868
mary = DataTree(children={"Sue": sue}) # noqa
6969
assert sue.name == "Sue"
7070

71-
@pytest.mark.xfail(reason="requires refactoring to retain name")
72-
def test_grafted_subtree_retains_name(self):
73-
subtree = DataTree("original")
74-
root = DataTree(children={"new_name": subtree}) # noqa
75-
assert subtree.name == "original"
76-
7771

7872
class TestPaths:
7973
def test_path_property(self):
@@ -294,8 +288,11 @@ class TestSetItem:
294288
def test_setitem_new_child_node(self):
295289
john = DataTree(name="john")
296290
mary = DataTree(name="mary")
297-
john["Mary"] = mary
298-
assert john["Mary"] is mary
291+
john["mary"] = mary
292+
293+
grafted_mary = john["mary"]
294+
assert grafted_mary.parent is john
295+
assert grafted_mary.name == "mary"
299296

300297
def test_setitem_unnamed_child_node_becomes_named(self):
301298
john2 = DataTree(name="john2")
@@ -304,10 +301,19 @@ def test_setitem_unnamed_child_node_becomes_named(self):
304301

305302
def test_setitem_new_grandchild_node(self):
306303
john = DataTree(name="john")
307-
DataTree(name="mary", parent=john)
304+
mary = DataTree(name="mary", parent=john)
308305
rose = DataTree(name="rose")
309-
john["Mary/Rose"] = rose
310-
assert john["Mary/Rose"] is rose
306+
john["mary/rose"] = rose
307+
308+
grafted_rose = john["mary/rose"]
309+
assert grafted_rose.parent is mary
310+
assert grafted_rose.name == "rose"
311+
312+
def test_grafted_subtree_retains_name(self):
313+
subtree = DataTree(name="original_subtree_name")
314+
root = DataTree(name="root")
315+
root["new_subtree_name"] = subtree # noqa
316+
assert subtree.name == "original_subtree_name"
311317

312318
def test_setitem_new_empty_node(self):
313319
john = DataTree(name="john")

‎datatree/tests/test_mapping.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def test_not_isomorphic_complex_tree(self, create_test_datatree):
6868
def test_checking_from_root(self, create_test_datatree):
6969
dt1 = create_test_datatree()
7070
dt2 = create_test_datatree()
71-
real_root = DataTree()
72-
real_root["fake_root"] = dt2
71+
real_root = DataTree(name="real root")
72+
dt2.name = "not_real_root"
73+
dt2.parent = real_root
7374
with pytest.raises(TreeIsomorphismError):
7475
check_isomorphic(dt1, dt2, check_from_root=True)
7576

‎docs/source/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ 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`).
41+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
4042

4143
Deprecations
4244
~~~~~~~~~~~~

0 commit comments

Comments
 (0)
This repository has been archived.