from typing import Mapping


def map_nested(func, node, path=[]):
    """Walk on a dictionary as a tree and call `func` on
    each leaf.

    Args:
        func: callable taking two arguments: the leaf and the path to it
            (path as list)
        node (collections.Mapping): the tree
        path: (for recursive use only)
    Returns:
        nested dict, on leaved of type returned by func.

    Source:
        https://stackoverflow.com/questions/32935232/python-apply-function-to-values-in-nested-dictionary
    """
    new_path = path.copy()
    if isinstance(node, Mapping):
        for k, v in node.items():
            return {
                k: map_nested(func, v, new_path + [k])
                for k, v in node.items()
            }
    else:
        return func(node, new_path)