benedict: cleanup code and add tests (#6380)

This commit is contained in:
Saugat Pachhai
2021-08-03 12:28:42 +05:45
committed by GitHub
parent 6c14bf39dc
commit 1eccfa9d33
2 changed files with 8 additions and 3 deletions
+7 -3
View File
@@ -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
+1
View File
@@ -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):