from typing import Any
from typing import get_origin
from typing import Union
from typing import get_args


def is_Optional_type(t: type):
    """ Check if the given type is Optional[] """
    # https://stackoverflow.com/questions/56832881/check-if-a-field-is-typing-optional
    # An Optional[] type is actually an alias for Union[..., NoneType]
    # This type has t.__origin__=typing.Union and t.__args__=[..., NoneType]
    # get_origin() won't fail: it returns None for non-typing types (Python 3.8+)
    return t is Any or (
        get_origin(t) is Union and
        type(None) in get_args(t)
    )