On this page
annotationlib
Prev: abc | Up: stdlib_module | Next: antigravity
Rel: typing, pydoc, pydoc_data, doctest, unittest
Ref: docs.python.org — annotationlib
Tags: #public #python3 #stdlib
Import
import annotationlib
Overview
Python 3.14+ deferred annotation introspection (get_annotations, Format, ForwardRef, call_annotate_function) implementing PEP 649 / PEP 749. Essential for runtime validators, CLI generators, and ORM/dataclass frameworks that inspect type hints safely even when forward references are unresolved.
Quick Start
import annotationlib
from annotationlib import Format
def greet(name: str, count: int = 1) -> str:
return name * count
val_ann = annotationlib.get_annotations(greet, format=Format.VALUE)
str_ann = annotationlib.get_annotations(greet, format=Format.STRING)
assert val_ann["name"] is str
assert str_ann["count"] == "int"
Implementation Walkthrough
1. Handling Forward References Safely (Format.FORWARDREF)
Under PEP 649, annotations are evaluated lazily on access. Requesting Format.FORWARDREF returns real objects where resolved and ForwardRef placeholders for undefined names instead of raising NameError:
import annotationlib
from annotationlib import Format
class Node:
def next_node(self) -> "MissingType": ...
hints = annotationlib.get_annotations(Node.next_node, format=Format.FORWARDREF)
assert isinstance(hints["return"], (str, annotationlib.ForwardRef))
Gotchas & Boundaries
- Python 3.14+ only: For code supporting Python 3.10–3.13 as well, fall back to
inspect.get_annotations()ortyping.get_type_hints(). - Choose the right
Format:Format.VALUEevaluates live objects,Format.FORWARDREFsubstitutes unresolved names safely, andFormat.STRINGreturns source-like strings without resolving names. - Avoid reading
obj.__annotations__directly when you want safe handling of deferred annotations—callannotationlib.get_annotations(obj, format=...).
Common Functions
- annotationlib.annotations_to_string() —
annotations_to_string(annotations)— Convert an annotation dict containing values to approximately the STRING format. - annotationlib.call_annotate_function() —
call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False)— Call an annotate function. annotate functions are normally generated by the compiler to defer the evaluation of annotations. They can be called with any of the format arguments in the Format enum, but - annotationlib.call_evaluate_function() —
call_evaluate_function(evaluate, format, *, owner=None)— Call an evaluate function. Evaluate functions are normally generated for the value of type aliases and the bounds, constraints, and defaults of type parameter objects. - annotationlib.get_annotate_from_class_namespace() —
get_annotate_from_class_namespace(obj)— Retrieve the annotate function from a class namespace dictionary. - annotationlib.get_annotations() —
get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=<Format.VALUE: 1>)— Compute the annotations dict for an object. - annotationlib.type_repr() —
type_repr(value)— Convert a Python value to a format suitable for use with the STRING format.
Classes & Objects
- annotationlib.Format —
annotationlib.Formatclass. (Methods:.as_integer_ratio(),.bit_count(),.bit_length(),.conjugate(),.from_bytes(),.is_integer()) - annotationlib.ForwardRef — Wrapper that holds a forward reference. (Methods:
.evaluate())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/abc
- tuts/python3/stdlib_module/annotationlib/annotations_to_string
- tuts/python3/stdlib_module/annotationlib/call_annotate_function
- tuts/python3/stdlib_module/annotationlib/call_evaluate_function
- tuts/python3/stdlib_module/annotationlib/Format
- tuts/python3/stdlib_module/annotationlib/ForwardRef
- tuts/python3/stdlib_module/annotationlib/get_annotate_from_class_namespace
- tuts/python3/stdlib_module/annotationlib/get_annotations
- tuts/python3/stdlib_module/annotationlib/type_repr
- tuts/python3/stdlib_module/antigravity
- tuts/python3/stdlib_module/bdb
- tuts/python3/stdlib_module/cProfile
- tuts/python3/stdlib_module/doctest
- tuts/python3/stdlib_module/faulthandler
- tuts/python3/stdlib_module/pdb
- tuts/python3/stdlib_module/profile
- tuts/python3/stdlib_module/pstats
- tuts/python3/stdlib_module/pydoc
- tuts/python3/stdlib_module/pydoc_data
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/timeit
- tuts/python3/stdlib_module/trace
- tuts/python3/stdlib_module/tracemalloc
- tuts/python3/stdlib_module/typing
- tuts/python3/stdlib_module/unittest
Related notes
- tuts/python3/stdlib_module/abc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/annotations_to_string Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/call_annotate_function Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/call_evaluate_function Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/Format Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/ForwardRef Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/get_annotate_from_class_namespace Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/get_annotations Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib/type_repr Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/antigravity Linked here; links to this page; shared tags: #stdlib