Annotations Are Descriptions

Lesson 48 of 54 · Python Foundations

A type hint records an expectation alongside code. It helps readers and optional tools understand the values a name or function is intended to use. Python remains dynamically typed: it does not automatically enforce these annotations when a function is called.

label: str = "north"
count: int = 3
ratio: float = 1.5
active: bool = True

def repeat_label(text: str, times: int) -> str:
    return text * times

print(repeat_label(label, count))
print(repeat_label("x", 2))
northnorthnorth
xx

A colon after a parameter or variable introduces its annotation; -> str describes the function’s return value. The hints do not convert inputs or change the multiplication. A tool may infer a variable’s type from its assignment, making some annotations redundant, but explicit function interfaces often provide useful context. A type checker can flag mismatches without running the program; exact warnings depend on the tool and its configuration.

An annotation must agree with the intended contract and implementation. If a function annotated -> int returns text, investigate which is wrong rather than assuming the annotation has made the result an integer. Runtime validation still matters at boundaries such as user input.