Skip to content

Commit cb91481

Browse files
authored
Add pre-commit hook that checks mypy version consistency (#615)
1 parent 3787c1a commit cb91481

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ repos:
1818
language: python
1919
always_run: true
2020
require_serial: true
21+
- repo: local
22+
hooks:
23+
- id: check-mypy-versions
24+
name: verify that pre-commits and dev env use the same mypy version
25+
entry: python .tools/check_mypy_versions.py
26+
language: python
27+
always_run: true
28+
require_serial: true
29+
additional_dependencies:
30+
- pyyaml
2131
- repo: local
2232
hooks:
2333
- id: update-algo-selection-code

.tools/check_mypy_versions.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import re
2+
import sys
3+
from pathlib import Path
4+
5+
import yaml
6+
7+
8+
def _get_mypy_version_from_precommit_config() -> str:
9+
config = yaml.safe_load(Path(".pre-commit-config.yaml").read_text())
10+
mypy_config = [
11+
hook
12+
for hook in config["repos"]
13+
if hook["repo"] == "https://github.com/pre-commit/mirrors-mypy"
14+
][0]
15+
match = re.search(r"v([\d.]+)", mypy_config["rev"]) # Remove "v" prefix
16+
if match:
17+
return match.group(1)
18+
raise ValueError("Mypy version not found in pre-commit config.")
19+
20+
21+
def _get_mypy_version_from_conda_environment() -> str:
22+
config = yaml.safe_load(Path("environment.yml").read_text())
23+
mypy_line = [dep for dep in config["dependencies"] if "mypy" in dep][0]
24+
match = re.search(r"mypy=([\d.]+)", mypy_line)
25+
if match:
26+
return match.group(1)
27+
raise ValueError("Mypy version not found in conda environment.")
28+
29+
30+
def main() -> None:
31+
v_precommit = _get_mypy_version_from_precommit_config()
32+
v_conda = _get_mypy_version_from_conda_environment()
33+
if v_precommit != v_conda:
34+
print(
35+
f"Error: Mypy versions do not match:\n"
36+
f" Pre-commit config: {v_precommit}\n"
37+
f" Conda environment: {v_conda}\n"
38+
)
39+
sys.exit(1)
40+
sys.exit(0)
41+
42+
43+
if __name__ == "__main__":
44+
main()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,6 @@ module = [
381381
"pdbp",
382382
"iminuit",
383383
"nevergrad",
384+
"yaml",
384385
]
385386
ignore_missing_imports = true

0 commit comments

Comments
 (0)