Recursively merge *updates* into a shallow copy of *base*. * Nested dicts are merged recursively. * ``None`` values in *updates* remove the corresponding key. * All other values overwrite.
(
base: dict[str, Any],
updates: dict[str, Any],
)
| 8 | |
| 9 | |
| 10 | def deep_merge( |
| 11 | base: dict[str, Any], |
| 12 | updates: dict[str, Any], |
| 13 | ) -> dict[str, Any]: |
| 14 | """Recursively merge *updates* into a shallow copy of *base*. |
| 15 | |
| 16 | * Nested dicts are merged recursively. |
| 17 | * ``None`` values in *updates* remove the corresponding key. |
| 18 | * All other values overwrite. |
| 19 | """ |
| 20 | result: dict[str, Any] = dict(base) |
| 21 | for key, value in updates.items(): |
| 22 | if value is None: |
| 23 | result.pop(key, None) |
| 24 | elif isinstance(value, dict) and isinstance(result.get(key), dict): |
| 25 | result[key] = deep_merge(result[key], value) |
| 26 | else: |
| 27 | result[key] = value |
| 28 | return result |
| 29 | |
| 30 | |
| 31 | def deep_merge_with_wholesale_keys( |