from typing import Dict
from typing import Any


def flatten_dict(
        nested_dict: Dict[str, Any],
        separator: str = ' ',
        prefix: str = ''
    ) -> Dict[str, Any]:
    """
    Takes a (possibly) nested dictionary and flattens it so that there is only one layer of keys.
    The keys of the flattened dictionary will be of the form 'parent_key + separator + child_key',
    with possibly multiple layers of nesting.

    :param nested_dict: The nested dictionary to flatten.
    :param separator: The separator to use in flattening the dictionary, be careful not to use
        something which may already be in a key such as an _.
    :param prefix: Used in the recursion only.
    :return: A flattened dictionary whose keys reflect the structure of the dictionary originally
        passed in.
    """
    # Use a dictionary comprehension with recursion to attain the flattened dictionary desired.
    # Code adapted from the link below.
    # https://www.geeksforgeeks.org/python-convert-nested-dictionary-into-flattened-dictionary
    return {prefix + separator + k if prefix else k: v
            for kk, vv in nested_dict.items()
            for k, v in flatten_dict(vv, separator, kk).items()
            } if isinstance(nested_dict, dict) else {prefix: nested_dict}