From e26d10dcd15ec6de3ad0b78efe0224a2f62b958b Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 21 Jan 2026 13:48:45 -0500 Subject: [PATCH] Use pyright in lint target; add type annotations Signed-off-by: mulhern --- .github/workflows/main.yml | 13 +++++-- Makefile | 1 + src/dbus_python_client_gen/_invokers.py | 48 ++++++++++++++++--------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 554d136..3414eac 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,8 +20,14 @@ jobs: task: make -f Makefile fmt-travis - dependencies: yamllint task: make -f Makefile yamllint - - dependencies: pylint python3-into-dbus-python python3-setuptools - task: PYTHONPATH=./src make -f Makefile lint + - dependencies: > + libatomic + pylint + python3-into-dbus-python + python3-setuptools + task: > + PATH=${PATH}:/github/home/.local/bin + PYTHONPATH=./src make -f Makefile lint - dependencies: python3-into-dbus-python python3-setuptools task: PYTHONPATH=./src make -f Makefile test - dependencies: > @@ -39,7 +45,10 @@ 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/Makefile b/Makefile index 1e0c7e8..8702104 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ lint: pylint setup.py pylint src/dbus_python_client_gen pylint tests --ignore=_introspect.py + pyright .PHONY: test test: diff --git a/src/dbus_python_client_gen/_invokers.py b/src/dbus_python_client_gen/_invokers.py index dd10780..f5c1d96 100644 --- a/src/dbus_python_client_gen/_invokers.py +++ b/src/dbus_python_client_gen/_invokers.py @@ -6,9 +6,12 @@ """ # isort: STDLIB import types +import xml.etree.ElementTree as ET # nosec B405 +from typing import Any, Callable, Mapping, MutableMapping, Sequence, Type # isort: THIRDPARTY import dbus +from dbus.proxies import ProxyObject # isort: FIRSTPARTY from into_dbus_python import IntoDPError, xformer, xformers @@ -24,7 +27,9 @@ ) -def prop_builder(interface_name, properties, timeout): +def prop_builder( + interface_name: str, properties: Sequence[ET.Element], timeout: int +) -> Callable[[MutableMapping[str, Type]], None]: """ Returns a function that builds a property interface based on arguments. @@ -51,14 +56,14 @@ def prop_builder(interface_name, properties, timeout): :raises DPClientGenerationError: """ - def build_property_getter(name): + def build_property_getter(name: str) -> Callable[[ProxyObject], Any]: """ Build a single property getter for this class. :param str name: the name of the property """ - def dbus_func(proxy_object): + def dbus_func(proxy_object: ProxyObject) -> Any: """ The property getter. @@ -82,7 +87,9 @@ def dbus_func(proxy_object): return dbus_func - def build_property_setter(name, signature): + def build_property_setter( + name: str, signature + ) -> Callable[[ProxyObject, Any], None]: """ Build a single property setter for this class. @@ -101,7 +108,7 @@ def build_property_setter(name, signature): fmt_str % (signature, name, interface_name) ) from err - def dbus_func(proxy_object, value): + def dbus_func(proxy_object: ProxyObject, value: Any) -> None: """ The property setter. @@ -120,7 +127,7 @@ def dbus_func(proxy_object, value): ) from err try: # pragma: no cover - return proxy_object.Set( + proxy_object.Set( interface_name, name, arg, @@ -138,7 +145,9 @@ def dbus_func(proxy_object, value): return dbus_func - def build_property(access, name, signature): + def build_property( + access: str, name: str, signature: str + ) -> Callable[[MutableMapping[str, Callable]], None]: """ Select among getter, setter, or both methods for a given property. @@ -151,7 +160,7 @@ def build_property(access, name, signature): if access == "read": getter = build_property_getter(name) - def prop_method_builder(namespace): + def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None: """ Attaches getter to namespace. """ @@ -160,7 +169,7 @@ def prop_method_builder(namespace): elif access == "write": # pragma: no cover setter = build_property_setter(name, signature) - def prop_method_builder(namespace): + def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None: """ Attaches setter to namespace """ @@ -170,7 +179,7 @@ def prop_method_builder(namespace): getter = build_property_getter(name) setter = build_property_setter(name, signature) - def prop_method_builder(namespace): + def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None: """ Attaches getter and setter to namespace """ @@ -179,7 +188,7 @@ def prop_method_builder(namespace): return prop_method_builder - def builder(namespace): + def builder(namespace: MutableMapping[str, Type]) -> None: """ Fills the namespace of the parent class with class members that are classes. Each class member has the name of a property, and each @@ -233,7 +242,9 @@ class has up to two static methods, a Get method if the property is return builder -def method_builder(interface_name, methods, timeout): +def method_builder( + interface_name: str, methods: Sequence[ET.Element], timeout: int +) -> Callable[[MutableMapping[str, Callable]], None]: """ Returns a function that builds a method interface based on 'spec'. @@ -256,7 +267,10 @@ def method_builder(interface_name, methods, timeout): :raises DPClientGenerationError: """ - def build_method(name, inargs): + def build_method( + name: str, + inargs: Sequence[ET.Element], + ) -> Callable[[ProxyObject, Mapping[str, Any]], Any]: """ Build a method for this class. @@ -295,7 +309,7 @@ def build_method(name, inargs): ) from err arg_names_set = frozenset(arg_names) - def dbus_func(proxy_object, func_args): + def dbus_func(proxy_object: ProxyObject, func_args: Mapping[str, Any]) -> Any: """ The method proper. @@ -356,7 +370,7 @@ def dbus_func(proxy_object, func_args): return dbus_func - def builder(namespace): + def builder(namespace: MutableMapping[str, Callable]) -> None: """ Fills the namespace of the parent class with class members that are methods. Each method takes a proxy object and a set of keyword @@ -395,7 +409,7 @@ def builder(namespace): return builder -def make_class(name, spec, timeout=-1): +def make_class(name: str, spec: ET.Element, timeout: int = -1) -> Type: """ Make a class, name, from the given spec. The class defines static properties and methods according to the spec. @@ -418,7 +432,7 @@ def make_class(name, spec, timeout=-1): ) prop_builder_arg = prop_builder(interface_name, spec.findall("./property"), timeout) - def builder(namespace): + def builder(namespace: MutableMapping[str, Type]) -> None: """ Fills the namespace of the parent class with two class members, Properties and Methods. Both of these are classes which themselves