Sub topic 3
Running mypy and Pyright on your code
This chapter assumes you can write Python 3.10+ functions and classes, use pip inside a virtual environment, and run commands in a terminal. You should also know how to write basic annotations, which the previous chapter covered. Here you install both checkers, run them, and read what they report.
Installing the checkers
Both tools install from PyPI. Install them into the same virtual environment as your project so they can find your dependencies.
python -m pip install mypy pyright
mypy src/
pyright src/Each command takes a file or directory and checks everything under it. The pyright package from PyPI is a thin wrapper that downloads the Node.js-based checker on first use, so the first run may take longer.
Checking a file
Save this as src/app.py:
def greet(name: str) -> str:
return "Hello, " + name
greet("Ada")
greet(42) # flagged by the checker: int is not strThe program would fail only when greet(42) runs, because "Hello, " + 42 raises a TypeError. The checkers report the problem without running anything.
Reading the output
Both tools print one line per problem: the file, the line number, a message, and a rule name.
- mypy prints an error code in square brackets, such as
[arg-type]. You use this code in targeted ignore comments later. - Pyright prints a severity (
error,warning, orinformation) and a rule name in parentheses, such asreportArgumentType.
Both messages say the same thing here: the argument has type int but the parameter expects str. Each tool ends with a summary count of errors.
Exit codes
Each checker exits with status 0 when it finds no errors and a non-zero status when it does. This is what lets a script or CI job fail on a type error. You can check it yourself:
mypy src/ && echo "clean" || echo "errors found"Pyright also exits non-zero on errors. Warnings do not fail the run unless you ask for that.
Common mistakes
- Checking with the wrong environment. If the checker runs outside your virtual environment, it cannot see installed libraries and reports missing imports. Activate the environment first.
- Expecting runtime protection. A checker only analyzes code. Python ignores annotations when the program runs, so a wrong type can still reach a function at runtime, for example through unannotated callers.
- Reading "no errors" as "correct". Unannotated functions are mostly skipped by mypy by default, so a clean result can mean little was checked. A later chapter covers this.
Exercise
You run mypy src/ in CI and the job passes, but a teammate says a function with no annotations contains a type bug. Why might mypy have reported nothing, and what does the exit status tell you?
Show answer
By default mypy does not check the bodies of functions that have no annotations, so the bug goes unseen. An exit status of 0 only means the checker found no errors in what it analyzed. It does not mean the whole code base is type-correct.
Exercise
Which command makes a shell script continue only if mypy finds no errors in src/?
Show answer
mypy src/ && echo "clean". The && runs the next command only when mypy exits with status 0.