Function Annotations in Python
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.
Annotate a parameter
param: type in the function definition attaches an expected type.
Annotate the return value
-> type after the parentheses documents what the function returns.
Nothing is enforced automatically
Passing the "wrong" type still runs without error at runtime.
External tools do the checking
Type checkers like mypy read these annotations separately, catching mismatches before the code even runs.
Annotations are inspectable
Available at runtime through the function’s __annotations__ attribute.
def add(a: int, b: int) -> int: return a + b print(add(3, 5))print(add("3", "5"))print(add.__annotations__)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.