from typing import Any, Dict def safe_dict_update(target: Dict[Any, Any], source: Dict[Any, Any]): """Updates a dict in-place with the values from another in a safe manner. If there are keys in both target and source that have values that are not equal an error will be raised. Args: target: The dict to update source: The dict to source new keys from """ for key, value in source.items(): if key in target and target[key] != value: raise Exception('Attempted to update dict with mismatched value.') target[key] = value