Skip to content

Commit

Permalink
Make sure that nested dotted keys are resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
uwefladrich committed Nov 25, 2023
1 parent 1f87ca4 commit 5ecae60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/scriptengine/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def __setitem__(self, key: Any, item: Any) -> None:
if k not in d or not isinstance(d[k], Mapping):
d[k] = {}
d = d[k]
d[keys[-1]] = item
# Make sure that nested dotted keys are resolved!
d[keys[-1]] = Context(item).data if isinstance(item, Mapping) else item

def __contains__(self, key: object) -> bool:
def iter_contains(data, subkey):
Expand Down
7 changes: 7 additions & 0 deletions tests/context/test_context_dotted_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def test_set_dotted_key():
assert c["foo"]["bar"] == 1


def test_set_nested_dotted_keys():
dotted_dict = {"f1": {"g1.h1": 1, "g2": {"h2.i1": 2}}}
expanded_dict = {"f1": {"g1": {"h1": 1}, "g2": {"h2": {"i1": 2}}}}
c = Context(dotted_dict)
assert c == expanded_dict


def test_set_int_key():
c = Context()
c[1] = "foo"
Expand Down

0 comments on commit 5ecae60

Please sign in to comment.