Sub topic 15
Where mypy and Pyright differ
This chapter assumes you can already run both checkers, read their output, and write annotations with unions, None, and generics. It covers where the two tools give different answers on the same code and how to decide what to do about that.
Why two checkers disagree
mypy and Pyright both implement the typing rules defined by the Python typing PEPs. Those rules leave room for judgment, so the tools differ in three places:
- Design choices the spec does not fix. Examples are how to infer the type of a list literal and what to do with unannotated functions.
- Defaults. Each tool ships with different strictness settings.
- Architecture. mypy is written in Python. Pyright is written in TypeScript and runs on Node.js. The
pyrightpackage on PyPI is a wrapper that fetches the Node-based tool for you.
Neither tool is more correct in general. When they disagree, the question is which behavior suits your project, and your CI should use one tool as the source of truth.
Differences in inference
The same code can reveal different types. Run both tools on this file:
items = [1, "a"]
reveal_type(items)
def add(a, b):
return a + b
result = add(1, 2)
reveal_type(result)| Expression | mypy | Pyright | Column |
|---|---|---|---|
| `items` | `list[object]` | `list[int | str]` |
| `result` | `Any` | `int` |
For the list literal, mypy computes a common supertype of the elements. Pyright keeps the union of the element types.
For add, mypy treats a function with no annotations as untyped, so its return value is Any. Pyright infers the return type from the body. Because the parameters are unannotated, it also tries call-site return type inference, re-evaluating the body with the argument types it sees at each call.
The printed format differs too. mypy prints Revealed type is "builtins.list[builtins.object]", with module-qualified names. Pyright prints Type of "items" is "list[int | str]" as an information message.
Unannotated function bodies
By default mypy skips checking the body of a function that has no annotations. This code produces no mypy error:
def broken():
count: int = "three"
return countPyright reports the assignment in the body. To get similar behavior from mypy, set check_untyped_defs = true. This is the first option most teams turn on when they adopt mypy, because it makes mypy look inside functions it would otherwise ignore.
Variables that start empty
mypy sometimes cannot infer a type from a single assignment such as x = []. It may report Need type annotation for "x" when it cannot work out the element type from later use in the same scope. Pyright infers the declared type of x from all assignments in scope, so it usually asks for less. The cost is that Pyright can accept code where an unknown element type propagates, which strict mode then reports.
Differences in narrowing
Both tools narrow on is None, isinstance, truthiness, and literal comparisons. Pyright has generally supported a wider set of narrowing forms and tends to adopt new typing features earlier. Examples include narrowing a tuple by len(t) == 2 and narrowing on in against a collection of literals. mypy has been closing this gap, but the exact set depends on the versions you run.
> The gap changes with each release, so do not rely on a fixed list.
When a narrowing form is unsupported in your checker, the usual workaround is an explicit isinstance check, a local variable assigned after the check, or a TypeGuard function. These workarounds also behave the same in both tools.
Differences in configuration
Both read pyproject.toml, but the models are different. mypy exposes many individual flags and groups them under strict. Pyright exposes named rules such as reportOptionalMemberAccess, each with a severity, and groups them under a typeCheckingMode of off, basic, standard, or strict. Recent Pyright versions default to standard.
This configuration applies a similar policy in both tools and exempts a legacy package:
[tool.mypy]
python_version = "3.12"
check_untyped_defs = true
warn_unused_ignores = true
files = ["src"]
[[tool.mypy.overrides]]
module = "legacy.*"
ignore_errors = true
[tool.pyright]
pythonVersion = "3.12"
typeCheckingMode = "standard"
reportUnnecessaryTypeIgnoreComment = true
include = ["src"]
ignore = ["src/legacy"]Notable points about this file:
- mypy uses
[[tool.mypy.overrides]]for per-module settings. Pyright uses path lists such asignore, and it also accepts per-file comments like# pyright: strict. warn_unused_ignoresin mypy andreportUnnecessaryTypeIgnoreCommentin Pyright both flag ignore comments that no longer suppress anything.- Pyright honors
# type: ignoreby default, so mypy-style ignores work in both tools. Only# pyright: ignore[...]is Pyright-specific.
Differences in ecosystem and tooling
| Area | mypy | Pyright |
|---|---|---|
| Written in | Python | TypeScript (needs Node.js) |
| Plugins | Supports plugins, for example `pydantic.mypy` and `django-stubs` | No plugin system |
| Caching | `.mypy_cache` plus the `dmypy` daemon | Fast full runs and a watch mode |
| Editor | Language server plugins | Powers Pylance in VS Code |
| Error identifiers | Codes such as `[assignment]` | Rules such as `reportAssignmentType` |
The plugin gap matters most for framework-heavy projects. A library like Django builds attributes at runtime. mypy can understand them through a plugin, while Pyright cannot. Pyright instead relies on standard mechanisms like dataclass_transform that a library can opt into.
Speed depends on project size, cache state, and hardware, so measure on your own codebase instead of trusting general claims.
Choosing a setup
A practical rule is to make your CI checker and your editor checker the same tool. If they differ, developers see errors in CI that the editor never showed, or the reverse. Some teams run both and accept the extra maintenance. If you do, treat the stricter result as the target and add ignores only where the two truly conflict.
Handling a disagreement
When one tool reports an error that the other does not, work through these steps:
- Reduce the code to the smallest snippet that shows the difference.
- Add
reveal_typeon the relevant expression and run both tools. - Compare the revealed types. The difference is usually in an inferred type, not in the rule that reported the error.
- Fix it by adding an annotation, which both tools respect equally.
- Use a targeted ignore only if the code is correct and the checker is wrong, and note the reason in the comment.
Adding an explicit annotation is the best fix because it removes the dependence on inference, which is where the tools differ most.
Exercise
A function def total(prices): return sum(prices) has no annotations. mypy reports no error for total(["a"]), while Pyright reports one. Give two reasons and name one way to make mypy check the body.
Show answer
mypy treats the function as untyped and returns Any for calls to it, so it does not check the call against the body. Pyright infers types from the body and call site and flags the bad argument. Setting check_untyped_defs = true makes mypy check function bodies. Annotating the function (prices: list[float]) makes both tools report the bad call.
Exercise
You run reveal_type([1, "a"]) and get list[object] from one tool and list[int | str] from the other. Which tool printed which, and how do you make both agree?
Show answer
mypy printed list[object] and Pyright printed list[int | str]. Annotate the variable, for example items: list[int | str] = [1, "a"], so neither tool has to infer the element type.