Skip to content

Commit

Permalink
pre-commit: run mypy in the main virtualenv
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
jonathanperret committed Sep 30, 2024
1 parent 1adb7fb commit 1263c05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 6 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ repos:
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear,flake8-sfs]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.9.0'
- repo: local
hooks:
- id: mypy
args: [--config-file=.pre-commit.mypy.ini]
additional_dependencies: [numpy]
name: check types with mypy
language: system
pass_filenames: false
entry: mypy src
files: ^src/.*.py$
6 changes: 0 additions & 6 deletions .pre-commit.mypy.ini

This file was deleted.

0 comments on commit 1263c05

Please sign in to comment.