Module lessons (1/4)
Type hints: annotating types
Parameter and return annotations, list[int] / dict[str, int], union | None, and static checking with mypy.
What you'll learn in this lesson
- Annotate parameters, return values, and local variables with modern type hints syntax (Python 3.10+).
- Use the native generics
list[int],dict[str, float],tuple[int, str]and the concise unionsint | None. - Distinguish between static analysis (
mypy,pyright, IDE) and Python runtime behavior. - Avoid the mistake of believing that type annotations automatically block the wrong types from running.
Overview
The type hints introduced in Python allow you to formally document the expected type of parameters, return values and attributes directly in the source code. They do not change the dynamic nature of Python at runtime: the interpreter does not raise errors if you pass a string where int was noted, but IDE and static type checkers (mypy,…
The syntax is nome: tipo for parameters and variables, and -> tipo_ritorno after the function parentheses. Python automatically saves this information in the __annotations__ dictionary.
Sections covered in this lesson
- 11. Write down the parameters, variables, and return values
- 22. Native Generic Collections (
list[...],dict[...]) - 33. Unions and optional values with
|andNone - 44.
Callable,AnyandLiteralfor expressive APIs - 5Check for understanding
- 6Test yourself
Unlock "Type hints: annotating types" and the full Python Course curriculum
Parameter and return annotations, list[int] / dict[str, int], union | None, and static checking with mypy.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.