Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge nested env keys when using import_from #1034

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/ploomber/env/envdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(self, source, path_to_here=None, defaults=None):
import_from = _get_import_from(raw_data, path_to_here)

if import_from:
raw_data = {**import_from, **raw_data}
raw_data = deep_merge(import_from, raw_data)

# check raw data is ok
validate.raw_data_keys(raw_data)
Expand Down Expand Up @@ -472,3 +472,14 @@ def find_tags_in_dict(d):
tags = tags | util.get_tags_in_str(k)

return tags


def deep_merge(a: dict, b: dict) -> dict:
result = deepcopy(a)
for bk, bv in b.items():
av = result.get(bk)
if isinstance(av, dict) and isinstance(bv, dict):
result[bk] = deep_merge(av, bv)
else:
result[bk] = deepcopy(bv)
return result
10 changes: 9 additions & 1 deletion tests/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,20 +936,28 @@ def test_import_from_another_file(tmp_directory):
Path('base.yaml').write_text("""
key: value
base: 42
nested:
key_1: value_1
key_2: value_2
""")

env = EnvDict(
{
'meta': {
'import_from': 'base.yaml'
},
'key': 'new_value'
'key': 'new_value',
'nested': {
'key_1': 'new_value_1',
},
},
path_to_here=os.getcwd(),
)

assert env['key'] == 'new_value'
assert env['base'] == 42
assert env['nested']['key_1'] == 'new_value_1'
assert env['nested']['key_2'] == 'value_2'
# this section is for config, and should not be visible
assert 'meta' not in env

Expand Down