From 49a14407c4fe42376ceb3b61f1880a29847ec3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20L=C3=B3pez?= Date: Thu, 16 May 2024 11:35:53 +0200 Subject: [PATCH] Pavel's review - 4 - Use Generics --- sssd_test_framework/utils/dbus/types.py | 34 ++++++++++++++----------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/sssd_test_framework/utils/dbus/types.py b/sssd_test_framework/utils/dbus/types.py index 88ec392b..05343963 100644 --- a/sssd_test_framework/utils/dbus/types.py +++ b/sssd_test_framework/utils/dbus/types.py @@ -4,6 +4,7 @@ import re from abc import ABC, abstractmethod +from typing import Generic, TypeVar __all__ = [ "DBUSResult", @@ -65,7 +66,10 @@ def __init__(self, text: str): self.text = text.replace("\n", "") -class DBUSType(ABC): +PythonType = TypeVar("PythonType") + + +class DBUSType(ABC, Generic[PythonType]): """ Abstract class to create classes that handle type conversion between D-Bus and Python. @@ -81,13 +85,13 @@ class DBUSType(ABC): @property @abstractmethod - def value(self): + def value(self) -> PythonType: """The Python value""" pass @value.setter @abstractmethod - def value(self, val): + def value(self, val: PythonType): pass def __str__(self) -> str: @@ -141,7 +145,7 @@ def _guess_next_type(cls, result: DBUSResult) -> DBUSType: raise TypeError("Couldn't guess the type") -class DBUSTypeInteger(DBUSType): +class DBUSTypeInteger(DBUSType[int]): """ Abstract integer class to create classes that handle type conversion between D-Bus and Python integers. @@ -242,7 +246,7 @@ class DBUSTypeUInt64(DBUSTypeInteger): _signed = False -class DBUSTypeDouble(DBUSType): +class DBUSTypeDouble(DBUSType[float]): _type_prefix = "double" @property @@ -263,7 +267,7 @@ def parse(self, result: DBUSResult) -> None: self._value = float(res.group(1)) -class DBUSTypeBoolean(DBUSType): +class DBUSTypeBoolean(DBUSType[bool]): _type_prefix = "boolean" @property @@ -282,12 +286,12 @@ def parse(self, result: DBUSResult) -> None: raise TypeError("Not a boolean") result.text = text[res.span()[1] :] - self._value = (res.group(1).lower() == "true") + self._value = res.group(1).lower() == "true" # Param: boolean:false|true -class DBUSTypeString(DBUSType): +class DBUSTypeString(DBUSType[str]): _type_prefix = "string" @property @@ -314,7 +318,7 @@ def parse(self, result: DBUSResult) -> None: # Param: string:"My string" -class DBUSTypeObjectPath(DBUSType): +class DBUSTypeObjectPath(DBUSType[str]): _type_prefix = "objpath" @property @@ -338,7 +342,7 @@ def parse(self, result: DBUSResult) -> None: # Param: objpath:/org/freedesktop/sssd/infopipe/Components/monitor -class DBUSTypeContainer(DBUSType): +class DBUSTypeContainer(DBUSType[PythonType]): """ Abstract class to group the container types together. """ @@ -346,7 +350,7 @@ class DBUSTypeContainer(DBUSType): pass -class DBUSTypeVariant(DBUSTypeContainer): +class DBUSTypeVariant(DBUSTypeContainer[PythonType]): """ Python representation for the D-Bus ``variant`` type. @@ -368,13 +372,13 @@ def __init__(self, model_element: DBUSType | None = None): """The element stored by the variant""" @property - def value(self): + def value(self) -> PythonType: if self._element is None: raise ValueError("No value set") return self._element.value @value.setter - def value(self, val): + def value(self, val: PythonType): if self._element is None: raise ValueError("No subtype set for Variant") self._element.value = val @@ -410,7 +414,7 @@ def param(self) -> str: return f"{self._type_prefix}:{self._element.param()}" -class DBUSTypeDict(DBUSTypeContainer): +class DBUSTypeDict(DBUSTypeContainer[dict]): _type_prefix = "dict" @@ -533,7 +537,7 @@ def param(self) -> str: return str -class DBUSTypeArray(DBUSTypeContainer): +class DBUSTypeArray(DBUSTypeContainer[list]): _type_prefix = "array" def __init__(self, model_element: DBUSType | None = None):