Skip to content

Commit

Permalink
fixed bug when merge would replace a map with a value
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed May 10, 2019
1 parent 9edeca1 commit 414d493
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def map_merge(dest, src):
ret = copy.deepcopy(dest)
for key, value in src.items():
if key in dest and isinstance(dest[key], dict):
ret[key] = Config.map_merge(dest[key], value)
if isinstance(value, dict):
ret[key] = Config.map_merge(dest[key], value)
else:
ret[key] = value
else:
ret[key] = src[key]
return ret
Expand Down
17 changes: 15 additions & 2 deletions omegaconf/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,34 @@ def test_merge1():


def test_merge2():
# replaces an element with an element
c1 = OmegaConf.from_string('{a: 1, b: 2}')
c2 = OmegaConf.from_string('{b: 3}')
c3 = OmegaConf.merge(c1, c2)
assert {'a': 1, 'b': 3} == c3


def test_merge3():
# replaces an element with a map
c1 = OmegaConf.from_string('{a: 1, b: 2}')
c2 = OmegaConf.from_string('{b: {c: 3}}')
c3 = OmegaConf.merge(c1, c2)
assert {'a': 1, 'b': {'c': 3}} == c3

def test_merge4():
# replaces a map with an element
c1 = OmegaConf.from_string('{b: {c: 1}}')
c2 = OmegaConf.from_string('{b: 1}')
c3 = OmegaConf.merge(c1, c2)
assert {'b': 1} == c3

def test_3way_merge():
c1 = OmegaConf.from_string('{a: 1, b: 2}')
c2 = OmegaConf.from_string('{b: 3}')
c3 = OmegaConf.from_string('{a: 2, c: 3}')

c4 = OmegaConf.merge(c1, c2, c3)
assert {'a': 2, 'b': 3, 'c': 3} == c4


def test_dir():
c = OmegaConf.from_string('a: b')
assert ['a'] == dir(c)
Expand Down

0 comments on commit 414d493

Please sign in to comment.