from typing import Dict
from typing import Hashable
from typing import Optional


def reverse_dict(d: Dict[Hashable, Hashable], inplace: bool = False) -> Optional[Dict[Hashable, Hashable]]:
    """Swap keys and values.

    Args:
        d: A dict to reverse.
        inplace: If True, perform the reversal inplace.

    Returns:
        A reversed version of `d`, or None if inplace is True.

    Examples:
        Reversing a dict with two elements.

        >>> from rics.utility.collections import reverse_dict
        >>> reverse_dict({"A": 0, "B": 1})
        {0: 'A', 1: 'B'}
    """
    ans = {}
    for key, value in d.items():
        ans[value] = key

    if inplace:
        d.clear()
        d.update(ans)
        return None
    else:
        return ans