Skip to content

Commit

Permalink
benedict: cleanup code and add tests (#6380)
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry committed Aug 3, 2021
1 parent 6c14bf3 commit 1eccfa9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions dvc/utils/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ def merge_params(src: Dict, to_update: Dict) -> Dict:
"""Recursively merges params with benedict's syntax support in-place."""
from benedict import benedict

data = benedict(src)
if src:
benedict(src).merge(to_update, overwrite=True)
data.merge(to_update, overwrite=True)
else:
# NOTE: the following line may seem like an unnecessary duplication
# data.merge might affect the `src` if it's not empty, so we cannot
# check `if src` later, as it may have been mutated already.
data.merge(to_update, overwrite=True)
# benedict has issues keeping references to an empty dictionary
# see: https://github.com/iterative/dvc/issues/6374.
# Also, passing to_update through benedict to expand the syntax.
src.update(benedict(to_update))
src.update(data)
return src


Expand Down
1 change: 1 addition & 0 deletions tests/unit/utils/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def test_merge_params(changes, expected):
[
[{"foo": "baz"}, {"foo": "baz"}],
[{"foo": "baz", "goo": "bar"}, {"foo": "baz", "goo": "bar"}],
[{"foo[1]": ["baz", "goo"]}, {"foo": [None, ["baz", "goo"]]}],
],
)
def test_merge_params_on_empty_src(changes, expected):
Expand Down

0 comments on commit 1eccfa9

Please sign in to comment.