The Annotated object
Type Hints
Python’s type hinting syntax can be used to annotate variables, function arguments, and return types with physical units. The following examples demonstrate the usage of type hints for annotating units:
# not compulsory
meters = Annotated[float, "meters"]
seconds = Annotated[float, "seconds"]
def speed(distance: "meters", time: "seconds") -> "meters / seconds":
return distance / time
Annotating a variable:
from typing_extensions import Annotated distance: Annotated[float, "meters"]
Annotating function arguments:
from typing_extensions import Annotated def speed(distance: Annotated[float, "meters"], time: Annotated[float, "seconds"]) -> Annotated[float, "meters per second"]: return distance / time
Annotating function return type:
from typing_extensions import Annotated def speed(distance: Annotated[float, "meters"], time: Annotated[float, "seconds"]) -> Annotated[float, "meters per second"]: return distance / time
Note
Impunity is not yet compatible with all the different syntaxes introduced in the PEP 484.