Recursively apply changes from src to dest. Preserves dest type and hidden info in dest structure, like ruamel.yaml leaves when parses files. This includes comments, ordering and line foldings. Used in Stage load/dump cycle to preserve comments and custom formatting.
(src, dest)
| 4 | |
| 5 | @no_type_check |
| 6 | def apply_diff(src, dest): # noqa: C901 |
| 7 | """Recursively apply changes from src to dest. |
| 8 | |
| 9 | Preserves dest type and hidden info in dest structure, |
| 10 | like ruamel.yaml leaves when parses files. This includes comments, |
| 11 | ordering and line foldings. |
| 12 | |
| 13 | Used in Stage load/dump cycle to preserve comments and custom formatting. |
| 14 | """ |
| 15 | Seq = (list, tuple) # noqa: N806 |
| 16 | Container = (Mapping, list, tuple) # noqa: N806 |
| 17 | |
| 18 | def is_same_type(a, b): |
| 19 | return any( |
| 20 | isinstance(a, t) and isinstance(b, t) for t in [str, Mapping, Seq, bool] |
| 21 | ) |
| 22 | |
| 23 | if isinstance(src, Mapping) and isinstance(dest, Mapping): |
| 24 | for key, value in src.items(): |
| 25 | if isinstance(value, Container) and is_same_type(value, dest.get(key)): |
| 26 | apply_diff(value, dest[key]) |
| 27 | elif key not in dest or value != dest[key]: |
| 28 | dest[key] = value |
| 29 | for key in set(dest) - set(src): |
| 30 | del dest[key] |
| 31 | elif isinstance(src, Seq) and isinstance(dest, Seq): |
| 32 | if len(src) != len(dest): |
| 33 | dest[:] = src |
| 34 | else: |
| 35 | for i, value in enumerate(src): |
| 36 | if isinstance(value, Container) and is_same_type(value, dest[i]): |
| 37 | apply_diff(value, dest[i]) |
| 38 | elif value != dest[i]: |
| 39 | dest[i] = value |
| 40 | else: |
| 41 | raise AssertionError( # noqa: TRY004 |
| 42 | f"Can't apply diff from {type(src).__name__} to {type(dest).__name__}" |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | def to_omegaconf(item): |