Skip to content

Dict Assign-Update to Union

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

Description

Changes dictionary assignments and updates to use the union operator.

Before

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

After

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

Explanation

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.