Sub topic 15
Where mypy and Pyright differ
This chapter assumes you can already run both checkers, read their errors, and use reveal_type. Both tools implement the same typing standards, so most correct code passes in both. They disagree in defaults, inference rules, and tooling, and those differences explain most of the surprises you will see.
Implementation and tooling
mypy is written in Python and installs with pip. Pyright is written in TypeScript and runs on Node.js. The pyright package from PyPI downloads Node for you. Pyright is also the engine behind the Pylance editor extension, so VS Code users already run it.
| Aspect | mypy | Pyright |
|---|---|---|
| Error identifiers | Error codes such as `assignment` | Rule names such as `reportAssignmentType` |
| Plugins | Supported (for example Django, SQLAlchemy) | Not supported |
| Speed on large projects | Relies on an incremental cache | Generally faster, with a built-in watch mode |
| Default strictness | Lenient | Stricter by default |
The identifier difference matters for ignore comments. # type: ignore[assignment] is a mypy-style comment. # pyright: ignore[reportAssignmentType] targets one Pyright rule.
Unannotated functions
By default mypy skips the body of a function that has no annotations. Pyright checks every body and infers return types.
def count(items):
total: int = "zero"
return totalmypy reports nothing for this function unless you pass --check-untyped-defs or enable strict. Pyright reports that str is not assignable to int. If you move a project from mypy to Pyright, expect new errors inside old untyped code.
Reassignment and inference
mypy fixes a variable's type at its first assignment. Pyright treats the declared type as the union of everything assigned and narrows on each assignment.
x = 1
x = "a"
reveal_type(x)mypy reports an incompatible assignment on line 2. Pyright accepts the code and reveals str at line 3. Empty containers differ in the same way. For items = [], mypy asks for an annotation ("Need type annotation"), while Pyright infers list[Unknown].
Do not output the empty diagram above; use the one below.
Practical advice
- Pick one tool as the source of truth in CI. Treat the other as advisory.
- If you run both, expect disagreements on edge cases and write ignore comments for the tool that fails the build.
- Compare
reveal_typeoutput from both tools when they disagree. The inferred types usually show the cause.
Exercise
A teammate's untyped function contains total: int = "zero". mypy with default settings reports no error, but Pyright does. Why, and how can you make mypy report it?
Show answer
By default mypy skips the bodies of functions that have no annotations, while Pyright checks them. Running mypy with --check-untyped-defs, or enabling strict, makes mypy check the body and report the error.