[elbe-devel] [PATCH] tests: add typechecking test using mypy

Thomas Weißschuh thomas.weissschuh at linutronix.de
Thu Jul 4 12:09:19 CEST 2024


Python has an optional, non-enforced typesystem.
It is useful for documentation and to avoid programming errors.
Add a test that can be enabled on a per-module basis to validate that
the present type-annotations are correct.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 debian/control    |  1 +
 pyproject.toml    |  6 ++++++
 test/test_mypy.py | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/debian/control b/debian/control
index 37b4f643d320..d08771691822 100644
--- a/debian/control
+++ b/debian/control
@@ -28,6 +28,7 @@ Build-Depends: debhelper-compat (= 13),
   python3-guestfs,
   python3-jsonschema,
   python3-libvirt,
+  python3-mypy,
   python3-passlib,
   python3-pytest,
   python3-sphinx,
diff --git a/pyproject.toml b/pyproject.toml
index 10be6f04e2b0..2a5d3f952a55 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -7,3 +7,9 @@ filename = 'docs/news/{project_date}-v{version}.rst'
 
 [tool.pytest.ini_options]
 filterwarnings = 'error'
+
+[tool.mypy]
+packages = [
+  'elbevalidate',
+]
+ignore_missing_imports = true
diff --git a/test/test_mypy.py b/test/test_mypy.py
new file mode 100644
index 000000000000..8c91db028e48
--- /dev/null
+++ b/test/test_mypy.py
@@ -0,0 +1,20 @@
+import importlib
+
+import pytest
+
+
+ at pytest.fixture
+def mypy_api():
+    try:
+        return importlib.import_module('mypy.api', package=__name__)
+    except ModuleNotFoundError as e:
+        if e.name == 'mypy':
+            pytest.skip(f'module {e.name} not found')
+        else:
+            raise
+
+
+def test_mypy(mypy_api):
+    normal_report, error_report, exit_status = mypy_api.run([])
+    if exit_status:
+        pytest.fail(normal_report, error_report)

---
base-commit: a9a4fd0be66f6dd9145a8a4fb5db3db510a83321
change-id: 20240704-typecheck-ced97233654c

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh at linutronix.de>



More information about the elbe-devel mailing list