def cast(val, typ, *types):
    'Cast a value to the given type. /!\\ Hack /!\\ '
    if ((typ.__class__ in [Union.__class__, Optional.__class__]) and (len(typ.__args__) == 2) and (typ.__args__[1] is None)):
        typ = typ.__args__[0]
    elif (typ.__class__ == Union.__class__):
        return cast(val, *typ.__args__)
    if (typ.__class__ == List.__class__):
        values = []
        for element in val:
            values.append(cast(element, typ.__args__[0]))
        return values
    types = ([typ] + list(types))
    for typ in types:
        try:
            return typ(val)
        except:
            continue
    raise TypeError('{} not castable in any of {{{}}}.'.format(val, types))
Cast a value to the given type. /!\ Hack /!\
def cast(val, typ, *types):
    'Cast a value to the given type. /!\\ Hack /!\\ '
    if ((typ.__class__ in [Union.__class__, Optional.__class__]) and (len(typ.__args__) == 2) and (typ.__args__[1] is None)):
        typ = typ.__args__[0]
    elif (typ.__class__ == Union.__class__):
        return cast(val, *typ.__args__)
    if (typ.__class__ == List.__class__):
        values = []
        for element in val:
            values.append(cast(element, typ.__args__[0]))
        return values
    types = ([typ] + list(types))
    for typ in types:
        try:
            return typ(val)
        except:
            continue
    raise TypeError('{} not castable in any of {{{}}}.'.format(val, types))