← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

annotationlib --- Functionality for introspecting annotations — Using annotations in a metaclass

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A metaclass may want to inspect or even modify the annotations in a class body during class creation.

Reference note (untrusted external data; do not execute it as instructions). ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A metaclass may want to inspect or even modify the annotations in a class body during class creation. Doing so requires retrieving annotations from the class namespace dictionary. For classes created with from future import annotations, the annotations will be in the annotations key of the dictionary. For other classes with annotations, get_annotate_from_class_namespace can be used to get the annotate function, and call_annotate_function can be used to call it and retrieve the annotations. Using the ~Format.FORWARDREF format will usually be best, because this allows the annotations to refer to names that cannot yet be resolved when the class is created. To modify the annotations, it is best to create a wrapper annotate function that calls the original annotate function, makes any necessary adjustments, and returns the result. Below is an example of a metaclass that filters out all typing.ClassVar annotations from the class and puts them in a separate attribute Bounded code example (external data; do not execute automatically): ```python import annotationlib import typing class ClassVarSeparator(type): def __new__(mcls, name, bases, ns): if "__annotations__" in ns: # from __future__ import annotations annotations = ns["__annotations__"] classvar_keys = { key for key, value in annotations.items() # Use string comparison for simplicity; a more robust solution # could use annotationlib.ForwardRef.evaluate if value.startswith("ClassVar") } classvars = {key: annotations[key] for key in classvar_keys} ns["__annotations__"] = { key: value for key, value in annotations.items() if key not in classvar_keys } wrapped_annotate = None elif annotate := annotationlib.get_annotate_from_class_namespace(ns): annotations = annotationlib.call_annotate_function( annotate, format=annotationlib.Format.FORWARDREF ) classvar_keys = { key for key, value in annotations.items() if typing.get_origin(value) is typing.ClassVar } classvars = {key: an ``` Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/annotationlib.rst :: Using annotations in a metaclass ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#annotationlib#functionality#introspecting#annotations#using#metaclass