From ca9463e0e0895e56555a14e07fbcb8094fadcfdb Mon Sep 17 00:00:00 2001
From: mulhern <amulhern@redhat.com>
Date: Mon, 2 Dec 2024 14:17:35 -0500
Subject: [PATCH] Use typing module for exactly one purpose

Add mypy and pyright to the lint tasks.

Configure mypy and pyright in pyproject.toml.

Signed-off-by: mulhern <amulhern@redhat.com>
---
 .github/workflows/main.yml       | 15 ++++++++++++---
 .github/workflows/weekly.yml     |  7 +++++--
 Makefile                         |  2 ++
 mypy.ini                         |  2 ++
 pyproject.toml                   |  3 +++
 src/into_dbus_python/_xformer.py | 13 +++++++++----
 6 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 mypy.ini

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 941ebdd..250c5ca 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -25,9 +25,10 @@ jobs:
               python3-dbus
               python3-dbus-signature-pyparsing
               python3-hypothesis
+              python3-mypy
             task: >
-              PYTHONPATH=./src:/github/home/.local/lib/python3.12/site-packages
-              make -f Makefile lint
+              PATH=${PATH}:/github/home/.local/bin
+              PYTHONPATH=./src make -f Makefile lint
           - dependencies: >
               python3-dbus
               python3-dbus-signature-pyparsing
@@ -53,6 +54,8 @@ jobs:
           ${{ matrix.dependencies }}
       - name: Install hs-dbus-signature
         run: pip install --user hs-dbus-signature
+      - name: Install pyright
+        run: pip install --user pyright
       - name: ${{ matrix.task }}
         run: ${{ matrix.task }}
 
@@ -66,7 +69,10 @@ jobs:
               python3-dbus-signature-pyparsing
               python3-hypothesis
               python3-hs-dbus-signature
-            task: PYTHONPATH=./src make -f Makefile lint
+              python3-mypy
+            task: >
+              PATH=${PATH}:/github/home/.local/bin
+              PYTHONPATH=./src make -f Makefile lint
           - dependencies: >
               python3-dbus
               python3-dbus-signature-pyparsing
@@ -90,6 +96,9 @@ jobs:
         run: >
           dnf install -y
           make
+          pip
           ${{ matrix.dependencies }}
+      - name: Install pyright
+        run: pip install --user pyright
       - name: ${{ matrix.task }}
         run: ${{ matrix.task }}
diff --git a/.github/workflows/weekly.yml b/.github/workflows/weekly.yml
index 1a0005e..6ea1828 100644
--- a/.github/workflows/weekly.yml
+++ b/.github/workflows/weekly.yml
@@ -21,9 +21,10 @@ jobs:
               python3-dbus
               python3-dbus-signature-pyparsing
               python-hypothesis
+              python3-mypy
             task: >
-              PYTHONPATH=./src:/github/home/.local/lib/python3.12/site-packages
-              make -f Makefile lint
+              PATH=${PATH}:/github/home/.local/bin
+              PYTHONPATH=./src make -f Makefile lint
     runs-on: ubuntu-latest
     container: fedora:41  # NEXT DEVELOPMENT ENVIRONMENT
     steps:
@@ -36,5 +37,7 @@ jobs:
           ${{ matrix.dependencies }}
       - name: Install hs-dbus-signature
         run: pip install --user hs-dbus-signature
+      - name: Install pyright
+        run: pip install --user pyright
       - name: ${{ matrix.task }}
         run: ${{ matrix.task }}
diff --git a/Makefile b/Makefile
index 6f467b9..4cb2d67 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@ lint:
 	pylint setup.py
 	pylint src/into_dbus_python
 	pylint tests
+	pyright
+	mypy src
 
 .PHONY: test
 test:
diff --git a/mypy.ini b/mypy.ini
new file mode 100644
index 0000000..d787271
--- /dev/null
+++ b/mypy.ini
@@ -0,0 +1,2 @@
+[mypy]
+disable_error_code = import-untyped
diff --git a/pyproject.toml b/pyproject.toml
index fed528d..6b0ad30 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,3 +1,6 @@
 [build-system]
 requires = ["setuptools"]
 build-backend = "setuptools.build_meta"
+
+[tool.pyright]
+include = ["src"]
diff --git a/src/into_dbus_python/_xformer.py b/src/into_dbus_python/_xformer.py
index b7a422a..8315e0f 100644
--- a/src/into_dbus_python/_xformer.py
+++ b/src/into_dbus_python/_xformer.py
@@ -18,6 +18,7 @@
 # isort: STDLIB
 import functools
 from collections.abc import Sequence
+from typing import Any
 
 # isort: THIRDPARTY
 import dbus
@@ -148,7 +149,7 @@ def the_dict_func(a_dict, *, variant=0):
         if len(toks) == 2:
             (func, sig) = toks[1]
 
-            def the_array_func(a_list, *, variant=0):
+            def the_array_func(a_list: Sequence[Any], *, variant=0):
                 """
                 Function for generating an Array from a list.
 
@@ -188,7 +189,7 @@ def _handle_struct(toks):
         signature = "".join(s for (_, s) in subtrees)
         funcs = [f for (f, _) in subtrees]
 
-        def the_func(a_list, *, variant=0):
+        def the_func(a_list: Sequence[Any], *, variant=0):
             """
             Function for generating a Struct from a list.
 
@@ -290,9 +291,13 @@ def __init__(self):
 
         self.VARIANT.setParseAction(self._handle_variant)
 
-        self.ARRAY.setParseAction(_ToDbusXformer._handle_array)
+        self.ARRAY.setParseAction(  # pyright: ignore [ reportOptionalMemberAccess ]
+            _ToDbusXformer._handle_array
+        )
 
-        self.STRUCT.setParseAction(_ToDbusXformer._handle_struct)
+        self.STRUCT.setParseAction(  # pyright: ignore [ reportOptionalMemberAccess ]
+            _ToDbusXformer._handle_struct
+        )
 
 
 _XFORMER = _ToDbusXformer()