Skip to content

Commit 6b4c17a

Browse files
committed
detect packages & modules to run on, allow non-strict mode
1 parent 2584e7f commit 6b4c17a

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ repos:
4949
hooks:
5050
- id: mypy
5151
name: mypy
52-
entry: "./run-mypy"
52+
entry: "python3 run_mypy.py"
5353
language: python
54-
additional_dependencies: ["mypy==0.790"]
54+
additional_dependencies: ["mypy==0.790", "tomlkit"]
5555
types: [python]
5656
# use require_serial so that script
5757
# is only called once per commit

run-mypy

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

run_mypy.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/python3
2+
# SPDX-FileCopyrightText: 2022 Jeff Epler, written for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: Unlicense
5+
"""Automatically run mypy. Use from pre-commit"""
6+
import os
7+
import pathlib
8+
import subprocess
9+
import tomlkit
10+
11+
12+
def print_check_call(command):
13+
"""Keep the user aware of commands being executed"""
14+
print("# Running", " ".join(command))
15+
subprocess.check_call(command)
16+
17+
18+
os.chdir(pathlib.Path(__file__).parent)
19+
20+
pip_command = ["pip", "install", "--no-input", "--quiet", "--editable", "."]
21+
print_check_call(pip_command)
22+
23+
with open("pyproject.toml") as f:
24+
meta = tomlkit.load(f)
25+
mypy_command = ["mypy"]
26+
if meta["tool"].get("adafruit", {}).get("mypy-strict", True):
27+
mypy_command.append("--strict")
28+
for module in meta["tool"]["setuptools"].get("py-modules", []):
29+
mypy_command.extend(["-m", module])
30+
for module in meta["tool"]["setuptools"].get("packages", []):
31+
mypy_command.extend(["-p", module])
32+
33+
print_check_call(mypy_command)

0 commit comments

Comments
 (0)