← All notes

Attach metadata to a type with typing.Annotated

Answer: Annotated is a special typing form to add context-specific metadata to an annotation. Write Annotated[T, x] to add metadata x to a given type T. PEP 593 introduces this mechanism to extend PEP 484 annotations with arbitrary metadata.

Static tools and runtime

Metadata added using Annotated can be used by static analysis tools or at runtime. The metadata is stored in a __metadata__ attribute. The typing docs also show that get_type_hints() can return an Annotated type, depending on how you call it.

What belongs in the metadata

PEP 484 is prescriptive about types. PEP 484 is prescriptive about types. Annotated[T, x] keeps T as the type and x as metadata.

| Annotated | metadata | |---|---| | Annotated[T, x] | Adds metadata x to type T | | __metadata__ | Attribute that stores the metadata at runtime | | static analysis tools | May consume the metadata | | PEP 593 | Extends PEP 484 annotations with arbitrary metadata |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 593 – Flexible function and variable annotations; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 593 – Flexible function and variable annotations; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0.

Cite this

Permanent ID: iil:note:ea3dyheq

iinventedlight. 2026. "Attach metadata to a type with typing.Annotated." iinventedlight Library, iil:note:ea3dyheq, version of 2026-11-19. https://iinventedlight.com/library/notes/python-typing-annotated/. License: CC-BY-4.0.

@misc{iil_note_ea3dyheq,
  author = {{iinventedlight}},
  title = {{Attach metadata to a type with typing.Annotated}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/python-typing-annotated/},
  note = {Permanent ID iil:note:ea3dyheq. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-typing-annotated.md.