Skip to content

Commit 95bbbaa

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 95bbbaa

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ 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

.pre-commit.mypy.ini

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)