def get_typing_function()

in nubia/internal/typing/builder.py [0:0]


def get_typing_function(tp):
    func = None

    # TypeVars are a problem as they can defined multiple possible types.
    # While a single type TypeVar is somewhat useless, no reason to deny it
    # though
    if is_typevar(tp):
        if len(tp.__constraints__) == 0:
            # Unconstrained TypeVars may come from generics
            func = _identity_function
        elif len(tp.__constraints__) == 1:
            assert not NEW_TYPING, "Python 3.7+ forbids single constraint for `TypeVar'"
            func = get_typing_function(tp.__constraints__[0])
        else:
            raise ValueError(
                "Cannot resolve typing function for TypeVar({constraints}) "
                "as it declares multiple types".format(
                    constraints=", ".join(
                        getattr(c, "_name", c.__name__) for c in tp.__constraints__
                    )
                )
            )
    elif tp == typing.Any:
        func = _identity_function
    elif issubclass_(tp, str):
        func = str
    elif is_mapping_type(tp):
        func = _apply_dict_type
    elif is_tuple_type(tp):
        func = _apply_tuple_type
    elif is_iterable_type(tp):
        func = _apply_list_type
    elif is_optional_type(tp):
        func = _apply_optional_type
    elif callable(tp):
        func = tp
    else:
        raise ValueError('Cannot find a function to apply type "{}"'.format(tp))

    args = getattr(tp, "__args__", None)

    if args:
        # this can be a Generic type from the typing module, like
        # List[str], Mapping[int, str] and so on. In that case we need to
        # also deal with the generic typing
        args_types = [get_typing_function(arg) for arg in args]
        func = _partial_builder(args_types)(func)

    return func