from typing import Dict
from typing import Union
from typing import cast


def hoist_section(
    config_dict: Dict[str, Dict[str, str]], section: str = "defaults"
) -> Dict[str, Union[Dict[str, str], str]]:
    """
        If a section exists called <hoist_section>, hoist it to the top level
        This is useful for overriding default configs from within a config file

        Transform `config_dict` to "hoist" any config values from a section
        into the top level (thus, overriding environment variables),
        when the name of the section matches `hoist_section`.

        Return a new, updated copy of `config_dict`.
    """

    if section not in config_dict.keys():
        # see https://github.com/python/mypy/issues/2300
        return cast(Dict[str, Union[Dict[str, str], str]], config_dict)

    hoisted_config_dict = cast(Dict[str, Union[Dict[str, str], str]], config_dict.copy())
    section_dict = config_dict[section].copy()
    del hoisted_config_dict[section]

    for k, v in section_dict.items():
        hoisted_config_dict[k.upper()] = v

    return hoisted_config_dict