Skip to content

Commit

Permalink
fix(utils.dicts): preserve empty dict leaf vals in deep merge (used i…
Browse files Browse the repository at this point in the history
…n kedro conf merge)
  • Loading branch information
lariel-fernandes committed Oct 21, 2024
1 parent 796bc23 commit cbcb5a0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/mlopus/utils/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ def deep_merge(*dicts: dict):
"""Merge dicts at the level of leaf-values."""
retval = {}

def _update(tgt: dict, src: dict, prefix_keys: List[str]):
def _update(tgt: dict, src: Mapping, prefix_keys: List[str]):
for key, val in src.items():
if isinstance(val, dict):
_update(tgt, val, prefix_keys + [key])
_key = prefix_keys + [key]

if isinstance(val, Mapping) and (val or isinstance(get_nested(tgt, _key, None), Mapping)):
# Treat value as nested if it's a non-empty dict or if the target is already nested
_update(tgt, val, _key)
else:
set_nested(tgt, prefix_keys + [key], deepcopy(val))
# Treat value as a leaf (scalar) otherwise
set_nested(tgt, _key, deepcopy(val))

for _dict in dicts:
_update(retval, _dict, prefix_keys=[])
Expand Down

0 comments on commit cbcb5a0

Please sign in to comment.