Skip to content

Dict Assign-Update to Union

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: dict-assign-update-to-union

Section titled “Sourcery refactoring id: dict-assign-update-to-union”

Changes dictionary assignments and updates to use the union operator.

def resolve_configs(
default_config: Dict[str, Any],
user_config: Dict[str, Any],
project_config: Dict[str, Any],
):
config = default_config.copy()
config.update(user_config)
config.update(project_config)
return config
def resolve_configs(
default_config: Dict[str, Any],
user_config: Dict[str, Any],
project_config: Dict[str, Any],
):
return default_config | user_config | project_config

The union operator was implemented for dictionaries in Python 3.9 (see PEP 584) and introduces a concise way to merge dictionaries without resorting to copy/update statements.

See also: use-dictionary-union.