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. It also assumes Python 3.10 or newer. By the end you will have one file that configures both tools, with per-module exceptions and without surprises from the default settings.
Why configure at all
Both checkers work without configuration, but the defaults differ and are tuned for running on a single file. Command-line flags are fine for experiments. In a real project they get lost, and a teammate or the CI server ends up running different checks than you do. A committed configuration gives everyone the same answer.
Both tools can read pyproject.toml, so you can keep one file for both. Each tool reads only its own table, [tool.mypy] or [tool.pyright], and ignores the other.
How each tool finds its configuration
mypy checks for a config file in this order: mypy.ini, .mypy.ini, pyproject.toml, setup.cfg. In pyproject.toml it uses the file only if it contains a [tool.mypy] table. The first file that qualifies wins, and the others are ignored. You can pass --config-file path to choose one explicitly.
Pyright looks in the project root for pyrightconfig.json first. If that file exists, Pyright uses it and does not read [tool.pyright] at all. Otherwise it reads pyproject.toml. You can point it at another location with --project.
A common gotcha: a leftover mypy.ini or pyrightconfig.json silently overrides your pyproject.toml settings. If a setting seems to have no effect, check for these files first.
A shared baseline
The examples below configure both tools to check the same directory against the same Python version.
[tool.mypy]
python_version = "3.12"
files = ["src"]
warn_unused_ignores = true
warn_redundant_casts = true
warn_unreachable = true
[tool.pyright]
pythonVersion = "3.12"
include = ["src"]
typeCheckingMode = "standard"
reportUnnecessaryTypeIgnoreComment = "warning"The two tables are written in different styles. mypy uses snake_case option names and Pyright uses camelCase. The paired options do similar jobs, but they are not identical.
| Purpose | mypy | Pyright |
|---|---|---|
| Target Python version | `python_version` | `pythonVersion` |
| What to check | `files` | `include` |
| What to skip | `exclude` (regular expressions) | `exclude` (glob patterns) |
| Overall strictness | `strict = true` | `typeCheckingMode` |
| Unused ignore comments | `warn_unused_ignores` | `reportUnnecessaryTypeIgnoreComment` |
typeCheckingMode accepts "off", "basic", "standard", and "strict". Recent Pyright versions default to "standard", which is slightly stricter than "basic". The next chapter covers strict mode in detail.
Choosing what to check
Both tools need to know which files are yours. Be explicit about this, because the default (everything under the current directory) can pick up virtual environments, build output, and generated code.
mypy's exclude is a regular expression matched against file paths, and TOML lets you write a list of them:
[tool.mypy]
files = ["src", "tests"]
exclude = [
"^build/",
"^src/legacy/generated_.*\\.py$",
]Pyright's exclude takes glob patterns, and it has a separate ignore list:
[tool.pyright]
include = ["src", "tests"]
exclude = ["**/node_modules", "build", "**/__pycache__"]
ignore = ["src/legacy/generated"]These two options behave differently, in both tools:
excludestops the tool from discovering files as check targets. If a checked file imports an excluded one, the excluded file is still analyzed so that types can flow through. Errors in it can still show up in some situations.- Pyright's
ignoresuppresses diagnostics for the listed paths even though the files are still analyzed. Use it for code you do not own.
Telling the checkers about your environment
Third-party imports can only be resolved if the checker can see the packages you installed. mypy checks the environment it was installed into, so install mypy in the same virtual environment as your dependencies, or use python -m mypy from that environment. Pyright runs as a separate program and finds an interpreter itself, so you may need to point it at your environment:
[tool.pyright]
venvPath = "."
venv = ".venv"
extraPaths = ["scripts"]If you see Import "requests" could not be resolved from Pyright when the package is installed, the wrong interpreter is almost always the cause. This is a configuration error, not a typing error.
Per-module settings
A single global setting rarely fits an entire codebase. In mypy you use overrides entries, one per group of modules:
[[tool.mypy.overrides]]
module = ["legacy.*"]
disallow_untyped_defs = false
check_untyped_defs = false
[[tool.mypy.overrides]]
module = ["yaml", "somevendor.*"]
ignore_missing_imports = trueThe module value is a list of module names. A trailing .* matches the package and everything under it. The second block silences missing-stub errors for specific libraries, which is safer than setting ignore_missing_imports globally. A global setting would also hide typos in import names.
Pyright has no per-module table with the same shape. Instead it offers two mechanisms. The first is a strict list, which applies strict mode to chosen paths while the rest of the project stays at the global mode:
[tool.pyright]
typeCheckingMode = "standard"
strict = ["src/core"]
reportMissingTypeStubs = falseThe second is executionEnvironments, a list of tables that assign different roots, Python versions, or extra paths to parts of the project:
[[tool.pyright.executionEnvironments]]
root = "src"
pythonVersion = "3.12"
[[tool.pyright.executionEnvironments]]
root = "scripts"
pythonVersion = "3.10"
extraPaths = ["src"]Both tools also accept file-level comments. These are useful for one-off cases, such as # pyright: strict or # pyright: basic at the top of a file for Pyright, and # mypy: disallow-untyped-defs for mypy.
Rule levels in Pyright
Pyright reports each category of problem under a rule name such as reportOptionalMemberAccess. You can set any rule to "none", "information", "warning", or "error". The values true and false also work, as shorthand for the mode's default severity and "none". mypy has no severity levels. An option either enables a check or it does not, and every finding is an error.
[tool.pyright]
reportMissingImports = "error"
reportUnusedImport = "warning"
reportPrivateUsage = "none"Precedence and verifying what is in effect
Command-line options override the config file. This is handy in CI for a one-off run, such as mypy --strict src/core, but it also means that a flag in a script can undo a setting in your file.
Pyright prints the configuration file it loaded when it starts. For mypy, a quick test is to add a deliberate error that your configuration should catch, run the checker, and confirm it appears. Remove the error afterwards.
Common mistakes
- Setting
python_versionto a version newer than the interpreter your code runs on. The checker then accepts syntax and library features that fail at runtime. - Leaving both
excludeand the defaultincludebehavior unset, so the checker scans.venvand produces thousands of unrelated errors. - Using a regular expression in Pyright's
exclude, or a glob in mypy's. The syntaxes are not interchangeable. - Enabling
ignore_missing_importsglobally. Scope it to named modules with an override.
Exercise
Your pyproject.toml sets typeCheckingMode = "strict" under [tool.pyright], but Pyright still reports only basic-level problems and none of the strict ones. A teammate suggests the setting is broken. What should you check first, and why?
Show answer
Check whether a pyrightconfig.json exists in the project root. When it exists, Pyright uses it and does not read [tool.pyright] from pyproject.toml, so the strict setting is never loaded. Pyright prints the path of the configuration file it loaded, which confirms which file is in effect.
Exercise
You want mypy to stay at its normal settings for the whole project but stop reporting missing-stub errors only for the library somevendor. Write the TOML.
Show answer
[[tool.mypy.overrides]] module = ["somevendor", "somevendor.*"] ignoremissingimports = true