Skip to content

Backport annotationlib.value_to_string #544

Open
@JelleZijlstra

Description

@JelleZijlstra

In 3.14, I added a function to annotationlib to format type-like values, so you get e.g. int instead of <type 'int'>: https://docs.python.org/3.14/library/annotationlib.html#annotationlib.value_to_string

I think this would be useful in typing-extensions too, so let's backport it.

I don't like the name very much though, and it might look even more confusing outside of annotationlib. We can still change it in CPython too, suggestions welcome.

For reference this is what it does:

def value_to_string(value):

    if isinstance(value, type):
        if value.__module__ == "builtins":
            return value.__qualname__
        return f"{value.__module__}.{value.__qualname__}"
    if value is ...:
        return "..."
    if isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
        return value.__name__
    return repr(value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions