Skip to content

Commit d1485e6

Browse files
author
anon
committed
fixup! add type hints for app.py
add Literal DeviceClass minor naming fix remove unnecessary assert
1 parent 9ea06c2 commit d1485e6

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

qubesadmin/app.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
except ImportError:
5757
has_qubesdb = False
5858

59+
DeviceClass = typing.Literal["mic", "block", "pci", "usb", "webcam"]
60+
5961

6062
class VMCollection:
6163
"""Collection of VMs objects"""
@@ -83,24 +85,24 @@ def refresh_cache(self, force: bool=False) -> None:
8385
if not force and self._vm_dict_initialized:
8486
return
8587
vm_list_data = self.app.qubesd_call("dom0", "admin.vm.List")
86-
new_vm_list = {}
88+
new_vm_dict = {}
8789
self._vm_dict_initialized = True
8890
# FIXME: this will probably change
8991
for vm_data in vm_list_data.splitlines():
9092
vm_name, props = vm_data.decode("ascii").split(" ", 1)
9193
vm_name = str(vm_name)
9294
props = props.split(" ")
93-
new_vm_list[vm_name] = dict(
95+
new_vm_dict[vm_name] = dict(
9496
[vm_prop.split("=", 1) for vm_prop in props]
9597
)
9698
# if cache not enabled, drop power state
9799
if not self.app.cache_enabled:
98100
try:
99-
del new_vm_list[vm_name]["state"]
101+
del new_vm_dict[vm_name]["state"]
100102
except KeyError:
101103
pass
102104

103-
self._vm_dict = new_vm_list
105+
self._vm_dict = new_vm_dict
104106
for name, vm in list(self._vm_objects.items()):
105107
if vm.name not in self._vm_dict:
106108
# VM no longer exists
@@ -224,14 +226,17 @@ def list_vmclass(self) -> list[Klass]:
224226
assert e in typing.get_args(Klass)
225227
return typing.cast(list[Klass], sorted(vmclass))
226228

227-
def list_deviceclass(self) -> list[str]: # TODO make Literal ?
229+
def list_deviceclass(self) -> list[DeviceClass]:
228230
"""Call Qubesd in order to obtain the device classes list"""
229231
deviceclasses = (
230232
self.qubesd_call("dom0", "admin.deviceclass.List")
231233
.decode()
232234
.splitlines()
233235
)
234-
return sorted(deviceclasses)
236+
for e in deviceclasses:
237+
assert e in typing.get_args(DeviceClass)
238+
239+
return typing.cast(list[DeviceClass], sorted(deviceclasses))
235240

236241
def _refresh_pool_drivers(self) -> None:
237242
"""

qubesadmin/base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,9 @@ def qubesd_call(self, dest: str | None, method: str,
9191
if dest.startswith("@dispvm:"):
9292
dest = dest[len("@dispvm:") :]
9393
else:
94-
# TODO what if `dest` remains None here ?
95-
# qubesd_call expects a non-None `dest` arg
9694
dest: QubesVM | None = getattr(self.app, "default_dispvm", None)
97-
assert dest is not None
9895
if dest:
99-
dest: str = dest.name
100-
assert isinstance(dest, str)
96+
dest = dest.name
10197
# have the actual implementation at Qubes() instance
10298
return self.app.qubesd_call(dest, method, arg, payload,
10399
payload_stream)

qubesadmin/device_protocol.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,6 @@ def _parse(
605605
backend = get_domain(backend_name)
606606
else:
607607
identity = representation
608-
assert backend is not None
609608

610609
port_id, _, devid = identity.replace(sep, ":").partition(":")
611610
if port_id == "_":

qubesadmin/vm/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, app, name, klass=None, power_state=None):
6767
self.firewall = qubesadmin.firewall.Firewall(self)
6868

6969
@property
70-
def name(self):
70+
def name(self) -> str:
7171
"""Domain name"""
7272
return self._method_dest
7373

@@ -83,7 +83,7 @@ def name(self, new_value):
8383
self._volumes = None
8484
self.app.domains.clear_cache()
8585

86-
def __str__(self):
86+
def __str__(self) -> str:
8787
return self._method_dest
8888

8989
def __lt__(self, other):

0 commit comments

Comments
 (0)