Sub topic 11
Configuring both checkers in pyproject.toml
This chapter assumes you can run mypy and pyright from the command line (covered earlier) and that your project already has a pyproject.toml. Putting both configurations in that one file keeps the settings next to your dependencies and gives every developer and CI job the same results.
One file, two tables
Each tool reads its own table in pyproject.toml. mypy reads [tool.mypy] and Pyright reads [tool.pyright]. The tables do not share settings, so you repeat the values that matter to both, such as the Python version and the source directory.
A minimal shared configuration
[tool.mypy]
python_version = "3.12"
strict = true
files = ["src"]
[tool.pyright]
pythonVersion = "3.12"
typeCheckingMode = "strict"
include = ["src"]With this file in place, running mypy or pyright with no arguments checks the src directory. You no longer pass paths on the command line.
The settings correspond like this:
| Purpose | mypy | Pyright |
|---|---|---|
| Target Python version | `python_version` | `pythonVersion` |
| What to check | `files` | `include` |
| Strictness | `strict = true` | `typeCheckingMode` |
| Skip paths | `exclude` | `exclude` |
Pyright's typeCheckingMode accepts "off", "basic", "standard" (the default), and "strict". mypy has no single mode names. It enables strictness through individual flags, and strict = true turns on a bundle of them.
Per-module settings
You rarely want the same rules everywhere. mypy supports overrides for specific modules:
[[tool.mypy.overrides]]
module = ["legacy.*"]
ignore_errors = truePyright takes a different approach. It lets you name paths that get strict checking, or paths whose diagnostics are hidden:
[tool.pyright]
include = ["src"]
strict = ["src/core"]
ignore = ["src/legacy"]Here src/core is held to strict rules even if the overall mode is lower, and errors from src/legacy are not reported.
Common mistakes
- Mismatched versions. If
python_versionandpythonVersiondiffer, the two tools may disagree about which syntax and standard library features exist. - A second config file. Pyright prefers
pyrightconfig.jsonoverpyproject.tomlwhen both exist, and mypy looks formypy.inifirst. A forgotten file can silently override your table. - Wrong key style. mypy uses
snake_casekeys. Pyright usescamelCasekeys. Copying a key between tables is a common source of ignored settings.
Exercise
You set strict = true under [tool.mypy], but one directory, src/legacy, produces hundreds of errors. Which mypy setting lets you quiet that directory without weakening the rest, and what is the closest Pyright equivalent?
Show answer
Add a [[tool.mypy.overrides]] entry with module = ["legacy.*"] and ignore_errors = true (adjust the module name to match your package). In Pyright, list the path under ignore in [tool.pyright], for example ignore = ["src/legacy"].