Skip to content

Commit

Permalink
Pavel's review - 4 - Use Generics
Browse files Browse the repository at this point in the history
  • Loading branch information
aplopez committed May 16, 2024
1 parent 470185a commit 49a1440
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions sssd_test_framework/utils/dbus/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import re
from abc import ABC, abstractmethod
from typing import Generic, TypeVar

__all__ = [
"DBUSResult",
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -242,7 +246,7 @@ class DBUSTypeUInt64(DBUSTypeInteger):
_signed = False


class DBUSTypeDouble(DBUSType):
class DBUSTypeDouble(DBUSType[float]):
_type_prefix = "double"

@property
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -338,15 +342,15 @@ 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.
"""

pass


class DBUSTypeVariant(DBUSTypeContainer):
class DBUSTypeVariant(DBUSTypeContainer[PythonType]):
"""
Python representation for the D-Bus ``variant`` type.
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 49a1440

Please sign in to comment.