Learn Python
>>> functions.function_annotations

Function Annotations in Python

#concept

Function annotations let you attach an expected type to a function's parameters and return value, written directly in the definition. Python doesn't enforce them at runtime — they're purely informational — but tools like type checkers, editors, and documentation generators read them to catch mistakes and improve autocomplete.

#how it works
1

Annotate a parameter

param: type in the function definition attaches an expected type.

2

Annotate the return value

-> type after the parentheses documents what the function returns.

3

Nothing is enforced automatically

Passing the "wrong" type still runs without error at runtime.

4

External tools do the checking

Type checkers like mypy read these annotations separately, catching mismatches before the code even runs.

5

Annotations are inspectable

Available at runtime through the function’s __annotations__ attribute.

#example
def add(a: int, b: int) -> int:
return a + b
 
print(add(3, 5))
print(add("3", "5"))
print(add.__annotations__)
Output
8
35
{'a': <class 'int'>, 'b': <class 'int'>, 'return': <class 'int'>}
1add(3, 5) works exactly as annotated, returning 8.
2add("3", "5") also runs, even though the arguments are strings, not ints — Python doesn’t enforce annotations, so + just concatenates the strings, giving "35".
3add.__annotations__ shows the annotations are stored as a dictionary on the function itself, available for tools (or curious code) to inspect.
#use cases
Documenting a function's expected inputs and output directly in its signature, for anyone reading the code
Enabling better autocomplete and inline hints in editors like VS Code and PyCharm
Catching type mismatches before running the code, using a separate tool like mypy
Working with modern frameworks like FastAPI, which read annotations to validate and parse incoming data automatically

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.