Skip to content

Commit b73d595

Browse files
authored
[RFC] Enable mypy typechecking on subset of files in pre-commit (#6704)
In triton-lang/triton#6497 and triton-lang/triton#6467 I took care to ensure the relevant modules typechecked successfully with mypy. Now I want to make sure they stay this way, so I'm proposing including `mypy` in the standard pre-commit flow. This way we can iteratively make different files typecheck without fear of losing progress. Example UX when creating type error: ``` > git diff --cached diff --git a/python/triton/knobs.py b/python/triton/knobs.py index dd29151..eab3cca17 100644 --- a/python/triton/knobs.py +++ b/python/triton/knobs.py @@ -106,7 +106,7 @@ class env_int(env_base[int, int]): def from_env(self, val: str) -> int: try: - return int(val) + return str(int(val)) except ValueError as exc: raise RuntimeError(f"Unable to use {self.key}={val}: expected int") from exc > git commit -m tst check for broken symlinks............................(no files to check)Skipped detect destroyed symlinks................................................Passed trim trailing whitespace.................................................Passed fix end of files.........................................................Passed check yaml...........................................(no files to check)Skipped check toml...........................................(no files to check)Skipped check python ast.........................................................Passed check for added large files..............................................Passed check for merge conflicts................................................Passed check that executables have shebangs.................(no files to check)Skipped check that scripts with shebangs are executable..........................Passed detect private key.......................................................Passed debug statements (python)................................................Passed ruff.....................................................................Passed yapf.....................................................................Passed clang-format.........................................(no files to check)Skipped mypy.....................................................................Failed - hook id: mypy - exit code: 1 python/triton/knobs.py:109: error: Incompatible return value type (got "str", expected "int") [return-value] Found 1 error in 1 file (checked 3 source files) Expand YAML anchors..................................(no files to check)Skipped ``` And example UX when changing file that isn't typechecked (even though the file has type errors): ``` > git diff --cached diff --git a/python/triton/language/core.py b/python/triton/language/core.py index 397b875..9339cde9b 100644 --- a/python/triton/language/core.py +++ b/python/triton/language/core.py @@ -97,6 +97,7 @@ def _unwrap_iterable(x): # right answer for whether the class constexpr defines __iter__, and # abc.Iterable doesn't work (at least not without some metaclass magic). try: + _a: str = 5 iter(x[0]) return x[0] except TypeError: > git commit -m tst check for broken symlinks............................(no files to check)Skipped detect destroyed symlinks................................................Passed trim trailing whitespace.................................................Passed fix end of files.........................................................Passed check yaml...........................................(no files to check)Skipped check toml...........................................(no files to check)Skipped check python ast.........................................................Passed check for added large files..............................................Passed check for merge conflicts................................................Passed check that executables have shebangs.................(no files to check)Skipped check that scripts with shebangs are executable..........................Passed detect private key.......................................................Passed debug statements (python)................................................Passed ruff.....................................................................Passed yapf.....................................................................Passed clang-format.........................................(no files to check)Skipped mypy.....................................................................Passed Expand YAML anchors..................................(no files to check)Skipped [danzimm/mypycheck 9709ff131] tst 1 file changed, 1 insertion(+) ``` N.B. In order to avoid duplicate inclusion/exclusion lists in `pyproject.toml` and `.pre-commit-config.yaml` I've disabled passing file names from pre-commit (since the CLI files override any lists in `pyproject.toml`), since if we only specify the exlusions in `pre-commit` running `mypy` directly wouldn't pick up that configuration.
1 parent 40b9889 commit b73d595

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ repos:
3939
hooks:
4040
- id: clang-format
4141

42+
- repo: https://github.com/pre-commit/mirrors-mypy
43+
rev: "v1.15.0"
44+
hooks:
45+
- id: mypy
46+
pass_filenames: false
47+
4248
# Expand YAML anchors in files used by github workflows, because github can't
4349
# do this itself. This lets us use anchors, which avoids code duplication.
4450
- repo: local

pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ requires = ["setuptools>=40.8.0", "cmake>=3.18", "ninja>=1.11.1", "pybind11>=2.1
33
build-backend = "setuptools.build_meta"
44

55
[tool.mypy]
6-
mypy_path="$MYPY_CONFIG_FILE_DIR/python"
6+
mypy_path = "$MYPY_CONFIG_FILE_DIR/python"
7+
files = [
8+
"python/triton/knobs.py",
9+
"python/triton/runtime/build.py",
10+
"python/triton/_utils.py",
11+
"python/test/unit/test_knobs.py",
12+
"python/test/unit/runtime/test_compilation_listener.py",
13+
]
14+
exclude = ["/build/"]
15+
follow_imports = "silent"
716

817
[tool.yapf]
918
based_on_style = "pep8"

0 commit comments

Comments
 (0)