Skip to content

Commit 3849127

Browse files
committed
Rewrite mypy precommit hook
The existing `pre-commit/mirrors-mypy` works by checking individual files and ignoring imports. This is against the whole idea of mypy and limits its benefits. Reimplement the hook to run mypy on all the files in an isolated environment. More on the topic: * python/mypy#13916 * https://jaredkhan.com/blog/mypy-pre-commit Additionally: * Move all the mypy config to pyproject.toml. * Lock the version of mypy. * Add types-PyYAML to dev requirements. * Ignore all `.venv*` folders to support using multiple venvs for different python versions. * Run pre-commit mypy during PR lint CI and ignore errors as fixes are in progress. * Disable the mypy pre-commit hook until it is fixed.
1 parent 3eb26d5 commit 3849127

File tree

6 files changed

+33
-23
lines changed

6 files changed

+33
-23
lines changed

.github/workflows/pr.yml

+3
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ jobs:
9999
pre-commit run --all-files flake8
100100
pre-commit run --all-files prettier
101101
pre-commit run --all-files check-yaml
102+
103+
# mypy issues fixes are in progress so, for now, ignore errors
104+
pre-commit run --all-files mypy || true

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ server.htpasswd
2424
server.authz
2525
server.authn
2626

27-
.venv
27+
.venv*
2828
venv
2929
.env
3030
.chroma

.pre-commit-config.yaml

+15-22
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,23 @@ repos:
3131
- "--extend-ignore=E203,E501,E503"
3232
- "--max-line-length=88"
3333

34-
- repo: https://github.com/pre-commit/mirrors-mypy
35-
rev: "v1.2.0"
34+
- repo: local
3635
hooks:
3736
- id: mypy
38-
args:
39-
[
40-
--strict,
41-
--ignore-missing-imports,
42-
--follow-imports=silent,
43-
--disable-error-code=type-abstract,
44-
--config-file=./pyproject.toml,
45-
]
46-
additional_dependencies:
47-
[
48-
"types-requests",
49-
"pydantic",
50-
"overrides",
51-
"hypothesis",
52-
"pytest",
53-
"pypika",
54-
"numpy",
55-
"types-protobuf",
56-
"kubernetes",
57-
]
37+
name: mypy
38+
entry: "./bin/run-mypy.sh"
39+
language: python
40+
stages: [manual] # run manually for all issues are fixed
41+
pass_filenames: false
42+
# use your preferred Python version
43+
language_version: python3.12
44+
# trigger for commits changing Python files
45+
types_or: [python, toml]
46+
# use require_serial so that script
47+
# is only called once per commit
48+
require_serial: true
49+
# print the number of files as a sanity-check
50+
verbose: true
5851

5952
- repo: https://github.com/pre-commit/mirrors-prettier
6053
rev: "v3.1.0"

bin/run-mypy.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
pip install -r requirements.txt -r requirements_dev.txt --no-input --quiet
6+
7+
mypy .

pyproject.toml

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ pythonpath = ["."]
5555

5656
[tool.mypy]
5757
ignore_errors = false
58+
disable_error_code = "type-abstract"
59+
ignore_missing_imports = false
60+
strict = false # disable strict mypy checks until we can fix all the errors
61+
exclude = "bin/.*"
62+
plugins = "numpy.typing.mypy_plugin"
5863

5964
[[tool.mypy.overrides]]
6065
module = ["chromadb.proto.*"]

requirements_dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ grpcio-tools
44
httpx
55
hypothesis<=6.98.9 # > Than this version has API changes we don't currently support
66
hypothesis[numpy]<=6.98.9
7+
mypy==1.10.0
78
mypy-protobuf
89
pre-commit
910
pytest
1011
pytest-asyncio
1112
setuptools_scm
1213
types-protobuf
14+
types-PyYAML
1315
types-requests==2.30.0.0

0 commit comments

Comments
 (0)