Skip to main content

How to Add Extra Information to a Python Type Hint with Annotated

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A type hint says what kind of value something is, such as a float. typing.Annotated adds more to that hint, such as the unit the number is in, without changing the type itself. Libraries read that extra information to validate or describe values. This guide attaches a unit to a temperature, with real output from Python 3.15.2.

Syntax​

Syntax
from typing import Annotated

<Name> = Annotated[<type>, <metadata>, ...]
PartWhat it means
<type>The real type, such as float or str
<metadata>One or more extra values of any kind: a string, a number, or an object a library understands
<Name> = ...Optional: a name for the annotated type, used in hints like any other type

Python's typing documentation: "Special typing form to add context-specific metadata to an annotation. Add metadata x to a given type T by using the annotation Annotated[T, x]. Metadata added using Annotated can be used by static analysis tools or at runtime. At runtime, the metadata is stored in a __metadata__ attribute." A tool that doesn't know the metadata "should ignore the metadata and simply treat the annotation as T".

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

temperature.py
from typing import Annotated, get_type_hints

Celsius = Annotated[float, "degrees Celsius"]


def warn_if_hot(temperature: Celsius) -> None:
if temperature > 30:
print("It's hot")


warn_if_hot(32.5)
print(get_type_hints(warn_if_hot, include_extras=True)["temperature"])
print(Celsius.__metadata__)

The temperature.py script in a macOS-style code window: Celsius is Annotated of float and the string degrees Celsius, warn_if_hot takes a Celsius, and the script calls it with 32.5 and prints the hint and its metadata.

warn_if_hot() uses the value as an ordinary float. The last two lines read the hint back: get_type_hints(..., include_extras=True) keeps the Annotated wrapper, and __metadata__ holds the extra values.

Step 2: Run It​

Terminal
uv run python temperature.py
Output
It's hot
typing.Annotated[float, 'degrees Celsius']
('degrees Celsius',)

The output in a terminal: It's hot, then typing.Annotated[float, 'degrees Celsius'], then the tuple ('degrees Celsius',).

The comparison temperature > 30 worked because the value is still a plain float. The unit travelled with the hint, where code that inspects hints, such as a validation library, can find it.

Common Mistakes​

MistakeWhat happensDo this instead
Giving Annotated only a type, as in Annotated[float]TypeError: Annotated[...] should be used with at least two arguments (a type and an annotation).Add at least one metadata value after the type
Calling get_type_hints(warn_if_hot) without include_extras=TrueIt returns <class 'float'>: the metadata is droppedPass include_extras=True
Expecting the metadata to check values by itselfPlain Python ignores itUse a library that reads it, or read it yourself

Troubleshooting​

SymptomLikely causeFix
AttributeError: type object 'float' has no attribute '__metadata__'The hint was read without include_extras=True, so it's the plain floatRead the hint with include_extras=True, or use the Annotated alias itself

FAQ​

Why not just write a comment?​

A comment is for people. Metadata in Annotated is a real value at run time, so libraries can use it. Pydantic, for example, reads constraints such as a minimum length from it.

Can I attach more than one piece of metadata?​

Yes: Annotated[float, "degrees Celsius", "from the roof sensor"]. __metadata__ then holds both, in order.

Conclusion​

Annotated[float, "degrees Celsius"] kept the temperature a float for the comparison, and get_type_hints(..., include_extras=True) and __metadata__ read the unit back.

Additional Resources​