Skip to content

Commit

Permalink
fix: update envs recursively for subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
pwwang committed Jan 17, 2024
1 parent 6c2af71 commit d9afc32
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pipen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ def update_dict(
Args:
parent: The parent dictionary
new: The new dictionary
depth: The depth to be copied.
Examples:
>>> parent = {"a": {"b": 1}}
>>> new = {"a": {"c": 2}}
>>> update_dict(parent, new)
>>> # {"a": {"b": 1, "c": 2}}
Returns:
The updated dictionary or None if both parent and new are None.
Expand All @@ -220,7 +227,16 @@ def update_dict(
return None

out = (parent or {}).copy()
out.update(new or {})
for key, val in (new or {}).items():
if (
key not in out
or not isinstance(val, dict)
or not isinstance(out[key], dict)
):
out[key] = val
else:
out[key] = update_dict(out[key], val)

return out


Expand Down
10 changes: 10 additions & 0 deletions tests/test_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,13 @@ def test_script_file_exists(pipen):
def test_invalid_name():
with pytest.raises(PipenOrProcNameError):
Proc.from_proc(SimpleProc, name="a b")


def test_inherit_proc_envs():
class Proc1(Proc):
envs = {"a": {"b": 1, "c": 2}}

class Proc2(Proc1):
envs = {"a": {"b": 2}}

assert Proc2.envs == {"a": {"b": 2, "c": 2}}
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def test_update_dict():
assert update_dict(None, None) is None
assert update_dict({}, None) == {}
assert update_dict(None, {}) == {}
assert update_dict({"a": 1}, {"b": 2}) == {"a": 1, "b": 2}
assert update_dict({"a": 1}, {"a": 2}) == {"a": 2}
assert update_dict({"a": {"b": 1}}, {"a": {"c": 2}}) == {
"a": {"b": 1, "c": 2}
}


def test_strsplit():
Expand Down

0 comments on commit d9afc32

Please sign in to comment.