Skip to content

Commit 3af168c

Browse files
authored
handle None comparison in deep_merge_dict (#494)
1 parent 5daba0e commit 3af168c

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

infrahub_sdk/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,18 @@ def deep_merge_dict(dicta: dict, dictb: dict, path: list | None = None) -> dict:
142142
if path is None:
143143
path = []
144144
for key in dictb:
145+
b_val = dictb[key]
145146
if key in dicta:
146-
if isinstance(dicta[key], dict) and isinstance(dictb[key], dict):
147-
deep_merge_dict(dicta[key], dictb[key], path + [str(key)])
148-
elif isinstance(dicta[key], list) and isinstance(dictb[key], list):
147+
a_val = dicta[key]
148+
if isinstance(a_val, dict) and isinstance(b_val, dict):
149+
deep_merge_dict(a_val, b_val, path + [str(key)])
150+
elif isinstance(a_val, list) and isinstance(b_val, list):
149151
# Merge lists
150152
# Cannot use compare_list because list of dicts won't work (dict not hashable)
151-
dicta[key] = [i for i in dicta[key] if i not in dictb[key]] + dictb[key]
152-
elif dicta[key] == dictb[key]:
153+
dicta[key] = [i for i in a_val if i not in b_val] + b_val
154+
elif a_val is None and b_val is not None:
155+
dicta[key] = b_val
156+
elif a_val == b_val or (a_val is not None and b_val is None):
153157
continue
154158
else:
155159
raise ValueError("Conflict at %s" % ".".join(path + [str(key)]))

tests/unit/sdk/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ def test_deep_merge_dict():
9696
c = {"keyB": {"sub2": 20}}
9797
d = {"keyA": [10, 20], "keyB": "foo"}
9898
e = {"keyA": [20, 30], "keyB": "foo"}
99+
f = {"keyA": None, "keyB": "bar"}
100+
g = {"keyA": "foo", "keyB": None}
99101
assert deep_merge_dict(a, b) == {"keyA": 1, "keyB": {"sub1": 10}}
100102
assert deep_merge_dict(c, b) == {"keyB": {"sub1": 10, "sub2": 20}}
101103
assert deep_merge_dict(d, e) == {"keyA": [10, 20, 30], "keyB": "foo"}
104+
assert deep_merge_dict(f, g) == {"keyA": "foo", "keyB": "bar"}
102105

103106

104107
def test_str_to_bool():

0 commit comments

Comments
 (0)