[tools] Fix merge_dicts for d2 immutability

This commit is contained in:
Kuba Szczodrzyński
2022-06-24 12:18:36 +02:00
parent c8fccdbb47
commit b371fd3468
3 changed files with 23 additions and 32 deletions

View File

@@ -6,14 +6,20 @@ from typing import Tuple, Union
SliceLike = Union[slice, str, int]
def merge_dicts(d1, d2, path=None):
if path is None:
path = []
for key in d2:
if key in d1 and isinstance(d1[key], dict) and isinstance(d2[key], dict):
merge_dicts(d1[key], d2[key], path + [str(key)])
else:
d1[key] = d2[key]
def merge_dicts(d1, d2):
if d1 is not None and type(d1) != type(d2):
raise TypeError("d1 and d2 are different types")
if isinstance(d2, list):
if d1 is None:
d1 = []
d1.extend(merge_dicts(None, item) for item in d2)
elif isinstance(d2, dict):
if d1 is None:
d1 = {}
for key in d2:
d1[key] = merge_dicts(d1.get(key, None), d2[key])
else:
d1 = d2
return d1