Sub topic 3
Checking your code with mypy or pyright
This chapter assumes you can annotate parameters and return values and know that the interpreter ignores hints at runtime. You also need to be able to install packages with pip.
What a static checker does
A static type checker reads your source files and compares each annotation with how the code uses it. It never runs the program. It reports mismatches as errors, so you find them before the code runs. Python itself does not do this check.
The two most common checkers are mypy and pyright. Both read the same annotation syntax. They differ in speed, defaults, and wording.
| Tool | Install | Run |
|---|---|---|
| mypy | `pip install mypy` | `mypy app.py` |
| pyright | `pip install pyright` | `pyright app.py` |
The pyright package from pip downloads the Node.js-based checker on first use.
Running a checker
Save this as app.py:
def add(a: int, b: int) -> int:
return a + b
total: int = add("x", "y")
print(total)Python runs this file without complaint. Now check it:
mypy app.pymypy reports something like this:
app.py:4: error: Argument 1 to "add" has incompatible type "str"; expected "int" [arg-type]
app.py:4: error: Argument 2 to "add" has incompatible type "str"; expected "int" [arg-type]
Found 2 errors in 1 file (checked 1 source file)Each line gives the file, the line number, a message, and an error code in brackets. Pyright words the same problem differently, for example Argument of type "Literal['x']" cannot be assigned to parameter "a" of type "int". Read the message in three parts: the value the checker found, the type it expected, and the place where they meet.
Reading errors
Errors fall into a few common groups:
arg-type: you passed a value of the wrong type to a function.return-value: the function returns something other than its declared type.assignment: you assigned a value that does not match a variable's annotation.union-attr(mypy) orreportOptionalMemberAccess(pyright): you used an attribute on a value that may beNone.
Fix the code or fix the annotation, whichever is wrong. Do not silence the error first.
Strictness and suppression
By default, mypy skips the body of any function that has no annotations. This lets you adopt hints gradually. To check more, turn on strict mode:
mypy --strict app.pyPyright has a similar setting, --level for output filtering and typeCheckingMode for strictness, set in pyproject.toml:
[tool.pyright]
typeCheckingMode = "strict"When you must silence one line, add a targeted comment:
value: int = "text" # type: ignore[assignment]Pyright also accepts # pyright: ignore. Name the error code so that the comment hides only that error.
Making it routine
Run the checker in your editor, in a pre-commit hook, and in continuous integration. A check that nobody runs finds nothing. Pin the checker version in your dev dependencies. Different versions report different errors, and an upgrade can fail a build that passed yesterday.
Exercise
A script has def double(n: int) -> int: return n * 2 and calls double("4"). Python runs it and returns 4444. What do you run to find the problem before executing, and what kind of error do you expect?
Show answer
Run mypy script.py or pyright script.py. The checker reports an argument type error (arg-type in mypy): the call passes a str where an int is expected.