from typing import cast from config.dtype import ArchMgrConfig def merge_configs(config: ArchMgrConfig, new_config: ArchMgrConfig) -> ArchMgrConfig: """Merge two configs, with the new_config taking precedence over the config in the conflicting fields with arrays and dicts merged Args: config: Base config new_config: Config to merge into the base config Returns: The merged config """ if len(new_config) == 0 or len(config) == 0: return config def combine(a: dict, b: dict): combined = {} for key in b: val = b[key] try: a[key] if isinstance(val, dict): combined[key] = combine(val, a[key]) elif isinstance(val, list): combined[key] = val for v in a[key]: combined[key].append(v) else: combined[key] = val except KeyError: combined[key] = val for key in a: try: b[key] except KeyError: combined[key] = a[key] return combined # Merge configs (using nasty casts) return cast(ArchMgrConfig, combine(cast(dict, config), cast(dict, new_config)))