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

Commit 8a9b7bf

Browse files
keewisTomNicholas
andauthored
update using dictionary unpacking (#213)
* merge two dictionaries using dictionary unpacking * check that the fix actually worked * use `|` to combine two dictionaries * Revert "use `|` to combine two dictionaries" This reverts commit ecfbbd5. --------- Co-authored-by: Tom Nicholas <[email protected]>
1 parent 3ddd44f commit 8a9b7bf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

datatree/datatree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ def update(self, other: Dataset | Mapping[str, DataTree | DataArray]) -> None:
878878

879879
vars_merge_result = dataset_update_method(self.to_dataset(), new_variables)
880880
# TODO are there any subtleties with preserving order of children like this?
881-
merged_children = OrderedDict(**self.children, **new_children)
881+
merged_children = OrderedDict({**self.children, **new_children})
882882
self._replace(
883883
inplace=True, children=merged_children, **vars_merge_result._asdict()
884884
)

datatree/tests/test_datatree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ def test_update_doesnt_alter_child_name(self):
235235
child = dt["a"]
236236
assert child.name == "a"
237237

238+
def test_update_overwrite(self):
239+
actual = DataTree.from_dict({"a": DataTree(xr.Dataset({"x": 1}))})
240+
actual.update({"a": DataTree(xr.Dataset({"x": 2}))})
241+
242+
expected = DataTree.from_dict({"a": DataTree(xr.Dataset({"x": 2}))})
243+
244+
print(actual)
245+
print(expected)
246+
247+
dtt.assert_equal(actual, expected)
248+
238249

239250
class TestCopy:
240251
def test_copy(self, create_test_datatree):

0 commit comments

Comments
 (0)