This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 5cfd705 chore: Revamp mypy config and publish typing metadata (#51)
5cfd705 is described below
commit 5cfd705e62e8ecd16e07fb9b3062eecee489eda9
Author: Junru Shao <[email protected]>
AuthorDate: Tue Sep 23 16:02:25 2025 -0700
chore: Revamp mypy config and publish typing metadata (#51)
- move mypy settings into pyproject.toml with project-wide targets,
namespace support, and overrides for tests/examples/optional deps
- replace per-path mypy hooks with a single project hook pinned to mypy
v1.18.2 and reuse shared stub deps
- add py.typed and bundle it in both wheels and sdists for PEP 561
consumers
After this PR is merged, developers could use vanilla argument-free
```
>>> mypy
```
to run repo-wide type checking.
---------
Co-authored-by: gemini-code-assist[bot]
<176961590+gemini-code-assist[bot]@users.noreply.github.com>
---
.pre-commit-config.yaml | 33 +++++++++------------------------
pyproject.toml | 18 +++++++++++++++++-
python/tvm_ffi/dataclasses/c_class.py | 10 ++--------
python/tvm_ffi/py.typed | 1 +
tests/lint/check_file_type.py | 1 +
5 files changed, 30 insertions(+), 33 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 41cabcc..db2d35f 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -104,29 +104,14 @@ repos:
- --config
- docs/.rstcheck.cfg
- repo: https://github.com/pre-commit/mirrors-mypy
- rev: "v1.17.0"
+ rev: "v1.18.2"
hooks:
- id: mypy
- name: mypy for `python/` and `tests/`
- additional_dependencies: ['numpy >= 1.22', "ml-dtypes >= 0.1",
"pytest", "typing-extensions>=4.5"]
- args: [--show-error-codes, --python-version=3.9]
- exclude: ^.*/_ffi_api\.py$
- files: ^(python/|tests/).*\.py$
- - id: mypy
- name: mypy for `examples/inline_module`
- additional_dependencies: ['numpy >= 1.22', "ml-dtypes >= 0.1",
"pytest", "typing-extensions>=4.5"]
- args: [--show-error-codes, --python-version=3.9]
- exclude: ^.*/_ffi_api\.py$
- files: ^examples/inline_module/.*\.py$
- - id: mypy
- name: mypy for `examples/packaging`
- additional_dependencies: ['numpy >= 1.22', "ml-dtypes >= 0.1",
"pytest", "typing-extensions>=4.5"]
- args: [--show-error-codes, --python-version=3.9]
- exclude: ^.*/_ffi_api\.py$
- files: ^examples/packaging/.*\.py$
- - id: mypy
- name: mypy for `examples/quick_start`
- additional_dependencies: ['numpy >= 1.22', "ml-dtypes >= 0.1",
"pytest", "typing-extensions>=4.5"]
- args: [--show-error-codes, --python-version=3.9]
- exclude: ^.*/_ffi_api\.py$
- files: ^examples/quick_start/.*\.py$
+ name: mypy
+ pass_filenames: false
+ args: []
+ additional_dependencies:
+ - numpy>=1.22
+ - ml-dtypes>=0.1
+ - pytest
+ - typing-extensions>=4.5
diff --git a/pyproject.toml b/pyproject.toml
index d51698f..e3ebf1c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -32,7 +32,7 @@ classifiers = [
]
keywords = ["machine learning", "inference"]
requires-python = ">=3.9"
-dependencies = ["typing-extensions"]
+dependencies = ["typing-extensions>=4.5"]
[project.urls]
Homepage = "https://github.com/apache/tvm-ffi"
@@ -115,6 +115,7 @@ sdist.include = [
"/python/tvm_ffi/**/*.py",
"/python/tvm_ffi/**/*.pyx",
"/python/tvm_ffi/**/*.pyi",
+ "/python/tvm_ffi/py.typed",
# Third party files
"/3rdparty/libbacktrace/**/*",
@@ -216,5 +217,20 @@ environment = { MACOSX_DEPLOYMENT_TARGET = "10.14" }
archs = ["AMD64"]
[tool.mypy]
+python_version = "3.9"
+show_error_codes = true
+mypy_path = ["python", "examples", "tests/python"]
+files = ["python/tvm_ffi", "examples", "tests/python"]
+namespace_packages = true
+explicit_package_bases = true
allow_redefinition = true
+exclude = '''(?x)(
+ ^\.venv/|
+ ^build/|
+ ^dist/|
+ ^\.mypy_cache/
+)'''
+
+[[tool.mypy.overrides]]
+module = ["torch", "torch.*", "my_ffi_extension", "my_ffi_extension.*"]
ignore_missing_imports = true
diff --git a/python/tvm_ffi/dataclasses/c_class.py
b/python/tvm_ffi/dataclasses/c_class.py
index 700f287..dc6aeed 100644
--- a/python/tvm_ffi/dataclasses/c_class.py
+++ b/python/tvm_ffi/dataclasses/c_class.py
@@ -27,18 +27,12 @@ from collections.abc import Callable
from dataclasses import InitVar
from typing import ClassVar, TypeVar, get_origin, get_type_hints
+from typing_extensions import dataclass_transform # type: ignore[attr-defined]
+
from ..core import TypeField, TypeInfo
from . import _utils
from .field import Field, field
-try:
- from typing_extensions import dataclass_transform # type:
ignore[attr-defined]
-except ImportError:
- from typing import dataclass_transform # type:
ignore[no-redef,attr-defined]
-except ImportError:
- pass
-
-
_InputClsType = TypeVar("_InputClsType")
diff --git a/python/tvm_ffi/py.typed b/python/tvm_ffi/py.typed
new file mode 100644
index 0000000..cbee111
--- /dev/null
+++ b/python/tvm_ffi/py.typed
@@ -0,0 +1 @@
+# Marker file for PEP 561. Keep this file empty.
diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py
index bedc542..b62e326 100644
--- a/tests/lint/check_file_type.py
+++ b/tests/lint/check_file_type.py
@@ -112,6 +112,7 @@ ALLOW_FILE_NAME = {
".gitmodules",
"CODEOWNERSHIP",
"Dockerfile",
+ "py.typed",
}
# List of specific files allowed in relpath to <proj_root>