Sub topic 17
Running checks in CI and in your editor
Prerequisites
This chapter assumes you have already:
- configured mypy or Pyright in
pyproject.toml(the configuration chapter), - chosen a strictness level and a rollout plan for an existing codebase (the strict mode and incremental adoption chapters),
- used Git and GitHub-style pull requests.
The goal here is to make the checker run automatically, in the same way for everyone on the team.
Two places, two jobs
A type checker is useful at two points in your workflow, and each point has a different job.
- The editor gives feedback while you type. Speed matters most. A slightly stale or lenient result is acceptable, because you will see the real result later.
- CI (continuous integration, a server that runs checks on every push or pull request) enforces the rules. Reproducibility matters most. The result must not depend on one person's machine.
If the editor is the only place types are checked, errors reach main whenever someone ignores a squiggle. If CI is the only place, developers learn about errors minutes after writing them, which is slow.
Running the checker in CI
Both tools signal failure through the process exit status. CI systems treat any non-zero status as a failed step, so you need no special integration. Both tools also read pyproject.toml. If you set files (mypy) or include (Pyright) as in the configuration chapter, the CI command can be a bare mypy or pyright.
A GitHub Actions workflow
name: typecheck
on:
pull_request:
push:
branches: [main]
jobs:
mypy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- run: python -m pip install -r requirements-dev.txt
- run: mypy
pyright:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- run: python -m pip install -r requirements-dev.txt
- run: pyrightYou only need one job if you chose one checker. The two are shown together because many teams run both while migrating.
Pin the checker version
Each release of mypy or Pyright can add checks, so a new release can turn a green build red without any change to your code. Pin both tools in the file that CI installs from:
mypy==1.13.0
pyright==1.1.389
types-requests==2.32.0.20241016Treat the pinned versions as examples and use the versions you have tested. Upgrade them in a dedicated pull request so new errors are easy to attribute.
Let the checker see your dependencies
A checker resolves import requests by looking in a Python environment. If CI does not install your dependencies, every third-party import becomes an "import not found" or an Any, and the check passes or fails for the wrong reasons. Install the project and its stub packages in the same environment the checker runs in.
- mypy analyzes against the interpreter that runs it. To point it elsewhere, use
--python-executable. - Pyright looks for the active environment. To point it elsewhere, set
venvPathandvenvin[tool.pyright], or pass--pythonpath.
Speed
mypy writes an incremental cache to .mypy_cache. In CI the cache starts empty unless you save it between runs. Caching that directory with actions/cache, keyed on the mypy version and your lock file, often cuts a run on a large codebase from minutes to seconds. Pyright has no on-disk cache of this kind, but it is usually fast on a cold start.
Locally, mypy offers a daemon that keeps the analysis in memory:
dmypy run -- src/
dmypy run -- src/
dmypy stopThe second call only re-checks files that changed. Use the daemon locally, not in CI, where a clean and reproducible run is what you want.
Running the checker before commit
A pre-commit hook catches errors before they reach CI. The standard hook repositories run mypy in an isolated environment, which cannot see your project's dependencies. A local hook that uses your own environment avoids that problem:
repos:
- repo: local
hooks:
- id: mypy
name: mypy
entry: mypy
language: system
types: [python]
pass_filenames: falsepass_filenames: false makes the hook run on the whole configured project. Checking only the changed files misses errors in unchanged files that depend on them, such as a caller of a function whose signature you just edited.
Running the checker in your editor
VS Code
VS Code gives you two routes.
- Pylance, the Python language server from Microsoft, uses the Pyright engine. It type-checks as you type. Its
python.analysis.typeCheckingModesetting acceptsoff,basic,standard, andstrict. - The Mypy Type Checker extension (
ms-python.mypy-type-checker) runs mypy and shows its messages in the Problems panel.
A workspace .vscode/settings.json that pins the interpreter and passes your config to mypy looks like this:
{
"python.defaultInterpreterPath": ".venv/bin/python",
"python.analysis.typeCheckingMode": "standard",
"mypy-type-checker.args": ["--config-file=pyproject.toml"]
}Commit this file so that the whole team gets the same behavior. If you also have a [tool.pyright] section or a pyrightconfig.json, Pylance reads it. Keep the settings consistent between the two, because a mismatch gives confusing results.
Other editors
- Neovim, Helix, Emacs, Sublime: Pyright ships a language server,
pyright-langserver, which any editor with LSP support can launch. mypy is usually added through a plugin or a linter integration that runs the command on save. - PyCharm: it has its own built-in type checker, which is a third implementation. A mypy plugin is available if you need CI parity.
Keeping the editor and CI in agreement
The most common complaint is "it's clean in my editor but red in CI," or the reverse. These are the usual causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Editor shows nothing, CI reports errors | Editor uses a different checker, for example Pylance while CI runs mypy | Use the same tool in both, or run both in CI deliberately |
| Imports unresolved in the editor only | Editor selected the wrong interpreter | Select the project's virtual environment |
| Errors appear after a version change | Pylance bundles its own Pyright version, which differs from your pin | Pin Pyright in CI and update the editor extension on a schedule |
| Local run passes, CI fails on stubs | `types-*` packages missing from the CI install | Put stub packages in the same requirements file |
| Strictness differs | Editor setting overrides or ignores `pyproject.toml` | Move the settings into the config file |
The most reliable rule is that CI is the source of truth, and the editor is a convenient preview of it. If you can pick only one checker for both, the setup is simpler. The earlier chapter on mypy and Pyright differences covers the behaviors that cause disagreement when you run both.
Making failures easy to act on
- Print enough context. By default mypy prints the error code in brackets, which you need for targeted
# type: ignore[code]comments. Pyright prints the rule name in parentheses, which you need for# pyright: ignore[rule]. - For a gradual rollout, fail the build only on new errors. A common approach is to keep the strict settings for a growing list of already clean modules in the config file, so that module-level overrides ratchet upward and the build fails if a clean module regresses.
- Avoid putting
|| trueafter the checker command to "see warnings without failing." The step then never fails, and the check becomes decoration.
Common errors
- Running the checker without installing the project. Result: many "Cannot find implementation or library stub" messages. Install dependencies first.
- Floating versions. Result: builds fail with no code change. Pin the versions.
- Checking only changed files in a hook. Result: cross-file errors reach CI. Check the whole project.
- Different Python versions. Result: errors from
python_versionorpythonVersiondiffering between the editor and CI. Set the version explicitly in the config.
Exercise
Your pre-commit hook for mypy uses a hook repository that runs in its own isolated environment. It reports "Cannot find implementation or library stub for module named requests" for code that CI accepts. Why does this happen, and what is a fix?
Show answer
The isolated environment has only mypy installed, so it cannot see your project's dependencies or types-* stub packages, and requests cannot be resolved. CI installs everything into one environment, so it works there. Fix it by using a local hook with language: system (and entry: mypy, pass_filenames: false), so mypy runs in your project's own virtual environment.
Exercise
A teammate sees no errors in VS Code with Pylance, but the CI job running mypy fails on the same branch. Give two possible reasons and one way to remove the mismatch.
Show answer
Pylance runs Pyright and CI runs mypy, and the two checkers differ in inference and narrowing. Alternatively, the editor may use a different strictness level or Python version than the one in the config file. To remove the mismatch, run the same checker in both places (for example, run Pyright in CI as well, or install the Mypy Type Checker extension), and put the strictness and Python version in pyproject.toml so that both read them.