Python Glossary Русский

Python 3.14+annotationlibfunction

annotationlib.annotations_to_string()

string representation

Turns a dict of already evaluated annotations into a dict of strings, as in STRING format; existing strings are kept as they are.

Syntax

annotationlib.annotations_to_string(annotations)

It is a helper for a custom __annotate__ function that must support STRING format but has no source text. Values go through type_repr, so int becomes 'int' rather than "<class 'int'>" as str() or repr() would give. A value that is already a string is kept without extra quotes. It does not help with forward references: its input is finished objects, so for annotations as written in source use get_annotations(..., format=Format.STRING).

Examples

# Python 3.14+
from annotationlib import annotations_to_string
ann = {'a': int, 'b': list[str], 'c': 'Later'}
print(annotations_to_string(ann))  # → {'a': 'int', 'b': 'list[str]', 'c': 'Later'}
print({k: repr(v) for k, v in ann.items()}['a'])  # → <class 'int'> — repr так не умеет

See also

Open in the full glossarydocs.python.orgReport an inaccuracy