Skip to content

Commit 95ed966

Browse files
pre-commit: run mypy in the main virtualenv
`pre-commit` will by default create an isolated virtualenv to run hooks such as `mypy`. This creates well-known issues because `mypy` won't have access to packages the code depends on. See python/mypy#13916 for recommendations by the `mypy` maintainer. By running `mypy` as a `system` hook as suggested, it will see the exact same packages as running `mypy` standalone (or through an IDE) will, which avoids discrepancies that appear only at commit time. The compromise is that running `mypy` through `pre-commit` will require a properly initialized virtualenv. However because we add a `files:` filter to the `mypy` hook, `pre-commit` will not run if no Python file has been changed; and if Python files have been changed it seems reasonable to expect a proper development environment has been set up. Also following the recommendation we set `pass_filenames: false` so that `pre-commit` does not pass only the changed files to `mypy`.
1 parent 99c6502 commit 95ed966

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

.pre-commit-config.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ repos:
1313
hooks:
1414
- id: flake8
1515
additional_dependencies: [flake8-bugbear,flake8-sfs]
16-
- repo: https://github.com/pre-commit/mirrors-mypy
17-
rev: 'v1.9.0'
16+
- repo: local
1817
hooks:
1918
- id: mypy
20-
args: [--config-file=.pre-commit.mypy.ini]
21-
additional_dependencies: [numpy]
19+
name: check types with mypy
20+
language: system
21+
pass_filenames: false
22+
entry: mypy src
23+
files: ^src/.*.py$

.pre-commit.mypy.ini

-6
This file was deleted.

0 commit comments

Comments
 (0)