from typing import Dict
from typing import Optional


def weighted_average(
    distribution: Dict[str, float],
    weights: Dict[str, float],
    rounding: Optional[int] = None,
) -> float:
    """
    Calculate a weighted average from dictionaries with the same keys, representing the values and the weights.

    Args:
        distribution: The values
        weights: The weights
        rounding: Whether to round off the result

    Returns:
        The weighted average

    """

    msg = "Attempting to calculate weighted average over two dictionaries that do not share keys"
    assert distribution.keys() == weights.keys(), msg
    numerator = sum([distribution[i] * weights[i] for i in distribution.keys()])
    denominator = sum(weights.values())
    fraction = numerator / denominator
    result = round(fraction, rounding) if rounding else fraction
    return result