| 1 | from typing import Any
|
| 2 |
|
| 3 | UNSET = object()
|
| 4 |
|
| 5 |
|
| 6 | def recursive_merge(*dictionaries: dict | None) -> dict:
|
| 7 | """Merge multiple dictionaries recursively.
|
| 8 |
|
| 9 | Later dictionaries take precedence over earlier ones.
|
| 10 | Nested dictionaries are merged recursively.
|
| 11 | UNSET values are skipped.
|
| 12 | """
|
| 13 | if not dictionaries:
|
| 14 | return {}
|
| 15 | result: dict[str, Any] = {}
|
| 16 | for d in dictionaries:
|
| 17 | if d is None:
|
| 18 | continue
|
| 19 | for key, value in d.items():
|
| 20 | if value is UNSET:
|
| 21 | continue
|
| 22 | if key in result and isinstance(result[key], dict) and isinstance(value, dict):
|
| 23 | result[key] = recursive_merge(result[key], value)
|
| 24 | else:
|
| 25 | result[key] = value
|
| 26 | return result
|
| 27 |
|