from typing import Callable
from typing import Any
from typing import Union


def none_or_type(target_type: type) -> Callable[[Any], Union[type, None]]:
    """
    Return a function which supports allowing an argparse argument to be ``None`` or a specific type.

    Paramaters
    ----------
    target_type : type
        Type that the returned function will attempt to cast given arguments to.

    Returns
    -------
    callable
        Takes in an argument. If the argument is ``None`` then ``None`` is returned. Otherwise,
        the argument is casted to the given type ``t``.
    """

    def _t_or_none(val: Any) -> Union[type, None]:
        return val if val is None else target_type(val)

    return _t_or_none