def nested_set(
    d: Dict[AnyKey, Any],
    key_paths: Tuple[List[AnyKey], ...],
    objs: Tuple[Any],
    allow_nonexistent: bool = False,
) -> Dict[AnyKey, Any]:
    """
    Set the values of a nested dictionary for the specified sequences of keys.

    :param d: Dict
        A nested dictionary.
    :param key_paths: Tuple[List]
        Each element of the tuple is a sequence of keys defining the path to an item of the nested dictionary. The
        length of the tuple must be the same as the length of `objs`, as to each key path the corresponding object in
        `objs` will be assigned.
    :param objs: Tuple
        Each element of the tuple is an object that will be assigned to the item specified by the corresponding
        sequence of keys.
    :param allow_nonexistent: bool
        Whether to create sequence of keys that are not found in the input dictionary or throw an exception.
    """
    if type(key_paths) != tuple:
        raise TypeError("`key_paths` must be a tuple.")
    if type(objs) != tuple:
        raise TypeError("`objs` must be a tuple.")
    if len(key_paths) != len(objs):
        raise ValueError("`key_paths` and `objs` must have the same length.")

    d0 = deepcopy(d)

    for keys, o in zip(key_paths, objs):
        error_msg = f"The sequence `keys={keys}` was not found in `d`."
        d2 = d0
        for key in keys[:-1]:
            if key in d2:
                d2 = d2[key]
            elif allow_nonexistent:
                d2[key] = {}
                d2 = d2[key]
            else:
                raise KeyError(error_msg)
        if keys[-1] in d2 or allow_nonexistent:
            d2[keys[-1]] = o
        else:
            raise KeyError(error_msg)
    return d0

    Set the values of a nested dictionary for the specified sequences of keys.

    :param d: Dict
        A nested dictionary.
    :param key_paths: Tuple[List]
        Each element of the tuple is a sequence of keys defining the path to an item of the nested dictionary. The
        length of the tuple must be the same as the length of `objs`, as to each key path the corresponding object in
        `objs` will be assigned.
    :param objs: Tuple
        Each element of the tuple is an object that will be assigned to the item specified by the corresponding
        sequence of keys.
    :param allow_nonexistent: bool
        Whether to create sequence of keys that are not found in the input dictionary or throw an exception.
    
fortuna/utils/nested_dicts.py