Coverage for src/python/dandeliion/client/tools/misc.py: 100%

1 from collections.abc import Mapping, MutableMapping

2 import copy

3

4

5# updates dict with varying depth

6 def update_dict(target: MutableMapping, updates: Mapping, inline: bool = True, force_none=False) -> MutableMapping:

7 if not inline:

8 target = copy.deepcopy(target)

9 for key, value in updates.items():

10 if (

11 value and # empty mapping or None

12 isinstance(value, Mapping) and

13 isinstance(target.get(key, None), Mapping)

14 ):

15 # recursive case

16 update_dict(target[key], value, inline=True, force_none=force_none)

17 elif value or not target.get(key, None) or force_none:

18 # set value if either not none or key does not exist yet or forcing overwrites by none

19 target[key] = value

20 if not inline:

21 return target