Skip to content

Commit 61e18ac

Browse files
authored
Merge pull request #118 from mulkieran/use-pyright
Use pyright in lint target; add type annotations
2 parents 66969ff + e26d10d commit 61e18ac

3 files changed

Lines changed: 43 additions & 19 deletions

File tree

.github/workflows/main.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ jobs:
2020
task: make -f Makefile fmt-travis
2121
- dependencies: yamllint
2222
task: make -f Makefile yamllint
23-
- dependencies: pylint python3-into-dbus-python python3-setuptools
24-
task: PYTHONPATH=./src make -f Makefile lint
23+
- dependencies: >
24+
libatomic
25+
pylint
26+
python3-into-dbus-python
27+
python3-setuptools
28+
task: >
29+
PATH=${PATH}:/github/home/.local/bin
30+
PYTHONPATH=./src make -f Makefile lint
2531
- dependencies: python3-into-dbus-python python3-setuptools
2632
task: PYTHONPATH=./src make -f Makefile test
2733
- dependencies: >
@@ -39,7 +45,10 @@ jobs:
3945
run: >
4046
dnf install -y
4147
make
48+
pip
4249
${{ matrix.dependencies }}
50+
- name: Install pyright
51+
run: pip install --user pyright
4352
- name: ${{ matrix.task }}
4453
run: ${{ matrix.task }}
4554

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ lint:
1313
pylint setup.py
1414
pylint src/dbus_python_client_gen
1515
pylint tests --ignore=_introspect.py
16+
pyright
1617

1718
.PHONY: test
1819
test:

src/dbus_python_client_gen/_invokers.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"""
77
# isort: STDLIB
88
import types
9+
import xml.etree.ElementTree as ET # nosec B405
10+
from typing import Any, Callable, Mapping, MutableMapping, Sequence, Type
911

1012
# isort: THIRDPARTY
1113
import dbus
14+
from dbus.proxies import ProxyObject
1215

1316
# isort: FIRSTPARTY
1417
from into_dbus_python import IntoDPError, xformer, xformers
@@ -24,7 +27,9 @@
2427
)
2528

2629

27-
def prop_builder(interface_name, properties, timeout):
30+
def prop_builder(
31+
interface_name: str, properties: Sequence[ET.Element], timeout: int
32+
) -> Callable[[MutableMapping[str, Type]], None]:
2833
"""
2934
Returns a function that builds a property interface based on arguments.
3035
@@ -51,14 +56,14 @@ def prop_builder(interface_name, properties, timeout):
5156
:raises DPClientGenerationError:
5257
"""
5358

54-
def build_property_getter(name):
59+
def build_property_getter(name: str) -> Callable[[ProxyObject], Any]:
5560
"""
5661
Build a single property getter for this class.
5762
5863
:param str name: the name of the property
5964
"""
6065

61-
def dbus_func(proxy_object):
66+
def dbus_func(proxy_object: ProxyObject) -> Any:
6267
"""
6368
The property getter.
6469
@@ -82,7 +87,9 @@ def dbus_func(proxy_object):
8287

8388
return dbus_func
8489

85-
def build_property_setter(name, signature):
90+
def build_property_setter(
91+
name: str, signature
92+
) -> Callable[[ProxyObject, Any], None]:
8693
"""
8794
Build a single property setter for this class.
8895
@@ -101,7 +108,7 @@ def build_property_setter(name, signature):
101108
fmt_str % (signature, name, interface_name)
102109
) from err
103110

104-
def dbus_func(proxy_object, value):
111+
def dbus_func(proxy_object: ProxyObject, value: Any) -> None:
105112
"""
106113
The property setter.
107114
@@ -120,7 +127,7 @@ def dbus_func(proxy_object, value):
120127
) from err
121128

122129
try: # pragma: no cover
123-
return proxy_object.Set(
130+
proxy_object.Set(
124131
interface_name,
125132
name,
126133
arg,
@@ -138,7 +145,9 @@ def dbus_func(proxy_object, value):
138145

139146
return dbus_func
140147

141-
def build_property(access, name, signature):
148+
def build_property(
149+
access: str, name: str, signature: str
150+
) -> Callable[[MutableMapping[str, Callable]], None]:
142151
"""
143152
Select among getter, setter, or both methods for a given property.
144153
@@ -151,7 +160,7 @@ def build_property(access, name, signature):
151160
if access == "read":
152161
getter = build_property_getter(name)
153162

154-
def prop_method_builder(namespace):
163+
def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None:
155164
"""
156165
Attaches getter to namespace.
157166
"""
@@ -160,7 +169,7 @@ def prop_method_builder(namespace):
160169
elif access == "write": # pragma: no cover
161170
setter = build_property_setter(name, signature)
162171

163-
def prop_method_builder(namespace):
172+
def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None:
164173
"""
165174
Attaches setter to namespace
166175
"""
@@ -170,7 +179,7 @@ def prop_method_builder(namespace):
170179
getter = build_property_getter(name)
171180
setter = build_property_setter(name, signature)
172181

173-
def prop_method_builder(namespace):
182+
def prop_method_builder(namespace: MutableMapping[str, Callable]) -> None:
174183
"""
175184
Attaches getter and setter to namespace
176185
"""
@@ -179,7 +188,7 @@ def prop_method_builder(namespace):
179188

180189
return prop_method_builder
181190

182-
def builder(namespace):
191+
def builder(namespace: MutableMapping[str, Type]) -> None:
183192
"""
184193
Fills the namespace of the parent class with class members that are
185194
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
233242
return builder
234243

235244

236-
def method_builder(interface_name, methods, timeout):
245+
def method_builder(
246+
interface_name: str, methods: Sequence[ET.Element], timeout: int
247+
) -> Callable[[MutableMapping[str, Callable]], None]:
237248
"""
238249
Returns a function that builds a method interface based on 'spec'.
239250
@@ -256,7 +267,10 @@ def method_builder(interface_name, methods, timeout):
256267
:raises DPClientGenerationError:
257268
"""
258269

259-
def build_method(name, inargs):
270+
def build_method(
271+
name: str,
272+
inargs: Sequence[ET.Element],
273+
) -> Callable[[ProxyObject, Mapping[str, Any]], Any]:
260274
"""
261275
Build a method for this class.
262276
@@ -295,7 +309,7 @@ def build_method(name, inargs):
295309
) from err
296310
arg_names_set = frozenset(arg_names)
297311

298-
def dbus_func(proxy_object, func_args):
312+
def dbus_func(proxy_object: ProxyObject, func_args: Mapping[str, Any]) -> Any:
299313
"""
300314
The method proper.
301315
@@ -356,7 +370,7 @@ def dbus_func(proxy_object, func_args):
356370

357371
return dbus_func
358372

359-
def builder(namespace):
373+
def builder(namespace: MutableMapping[str, Callable]) -> None:
360374
"""
361375
Fills the namespace of the parent class with class members that are
362376
methods. Each method takes a proxy object and a set of keyword
@@ -395,7 +409,7 @@ def builder(namespace):
395409
return builder
396410

397411

398-
def make_class(name, spec, timeout=-1):
412+
def make_class(name: str, spec: ET.Element, timeout: int = -1) -> Type:
399413
"""
400414
Make a class, name, from the given spec.
401415
The class defines static properties and methods according to the spec.
@@ -418,7 +432,7 @@ def make_class(name, spec, timeout=-1):
418432
)
419433
prop_builder_arg = prop_builder(interface_name, spec.findall("./property"), timeout)
420434

421-
def builder(namespace):
435+
def builder(namespace: MutableMapping[str, Type]) -> None:
422436
"""
423437
Fills the namespace of the parent class with two class members,
424438
Properties and Methods. Both of these are classes which themselves

0 commit comments

Comments
 (0)