def type_repr()

in chz/tiepin.py [0:0]


def type_repr(typ) -> str:
    # Similar to typing._type_repr
    if isinstance(typ, (types.GenericAlias, typing._GenericAlias)):
        if typ.__origin__.__module__ in {"typing", "typing_extensions", "collections.abc"}:
            if typ.__origin__ is collections.abc.Callable:
                return repr(typ).removeprefix("collections.abc.").removeprefix("typing.")

            # Based on typing._GenericAlias.__repr__
            name = typ.__origin__.__name__
            if typ.__args__:
                args = ", ".join([type_repr(a) for a in typ.__args__])
            else:
                args = "()"
            return f"{name}[{args}]"

        return repr(typ)

    if isinstance(typ, (type, types.FunctionType)):
        module = getattr(typ, "__module__", None)
        name = getattr(typ, "__qualname__", None)
        if name is None:
            name = getattr(typ, "__name__", None)
        if name is not None:
            if module == "typing":
                return f"{module}.{name}"
            if module is not None and module != "builtins" and module != "__main__":
                return f"{module}:{name}"
            return name

    if typ is ...:
        return "..."
    return repr(typ)