Python Glossary Русский

Python 3.14+annotationlibfunction

annotationlib.type_repr()

string representation

A string form of a value in annotation style: a class becomes its name without <class ...>, anything else gets the usual repr().

Syntax

annotationlib.type_repr(value)

Builtin types are rendered by short name ('int'), user classes with their module ('__main__.Point'), so the result depends on where the class is defined. A string gets quotes like repr() does, unlike annotations_to_string, which leaves strings alone. Handy for error messages and debug output of types: str(list[int]) is already nice, str(int) is not.

Examples

# Python 3.14+
from annotationlib import type_repr
class Point: ...
print(type_repr(int), str(int))  # → int <class 'int'>
print(type_repr(Point))  # → __main__.Point
print(type_repr(int | None), type_repr(...), type_repr('x'))  # → int | None ... 'x'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy