Sub topic 1
What static type checking is and why Python has it
Prerequisites
This chapter assumes you can write and run Python 3 functions, classes, and modules. It also assumes you can install packages with pip in a virtual environment and use the command line. You need Python 3.10 or newer, so that the str | None syntax works at runtime.
What a static type checker does
A static type checker is a program that reads your source code and reports type errors without running it. It works from type annotations, which are labels you add to say what kind of value a variable or function parameter holds. The checker compares those labels with how you use the values.
def greet(name: str) -> str:
return "Hello, " + name
greet("Ada")
greet(42) # flagged by the checker: int is not strThe annotations declare that greet takes a str and returns a str. Both mypy and Pyright report the call with an int argument before the program runs. Without a checker, Python would only fail when it reached that line and tried to add a string and an integer.
Why Python has it
Python is dynamically typed. A value has a type at runtime, but names and parameters have no fixed type. This keeps small scripts quick to write. In a large codebase it lets mistakes hide until the code path runs, such as a function that sometimes returns None and breaks its caller later.
Python added annotation syntax in version 3.0 and a standard typing module in 3.5. The interpreter does not enforce annotations. It stores them and carries on. Separate tools, the checkers, read them. This split is why mypy and Pyright exist as independent programs.
Gradual typing
Python uses gradual typing. You can annotate some parts of a program and leave others unannotated. The checker verifies what it can and treats unannotated parts as unknown. You can add annotations over time, starting with the code that matters most. A later chapter covers this in detail.
What a checker cannot guarantee
A checker proves that your code is consistent with its annotations. It does not prove the program is correct, and it does not check values at runtime.
- Data from outside the program, such as JSON from a network call, is only as typed as you declare it. If you annotate it wrongly, the checker believes you.
- Annotations are not enforced when the program runs. Calling
greet(42)through code the checker never saw still raises an error only when the addition executes. - Unannotated code and
Anyvalues reduce what the checker can verify.
The checking workflow
The checker runs as a separate step. You edit, check, fix, and only then run or deploy. Later chapters show how to run this loop from the command line, in CI, and in your editor.
Exercise
A function is annotated def total(prices: list[float]) -> float. A caller passes a list of strings, but the call sits in a module that no test imports. Will a static type checker report the problem, and will Python report it at runtime without the checker?
Show answer
The checker reports it, because it reads the whole codebase and compares the argument type with the annotation, with no need to run the code. Python itself does not enforce annotations. It would fail only if the module ran and the strings caused an error, such as in an addition with a float.