Skip to content
Snippets Groups Projects
Commit 49bc28c5 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Change MethodT

parent 7268bf6e
No related branches found
No related tags found
1 merge request!1First try of type hints
...@@ -33,12 +33,9 @@ if typing.TYPE_CHECKING: ...@@ -33,12 +33,9 @@ if typing.TYPE_CHECKING:
from .engine import Engine from .engine import Engine
from .core import EngineMixin from .core import EngineMixin
# Funtion type with any arguments and return a dict or None (device methods) # Funtion types with any arguments and return a dict or None (device methods)
MethodT = TypeVar( SyncMethodT = Callable[..., Union[dict, None]] # Fonction synchrone qui retourne un dict ou None
"MethodT", AsyncMethodT = Callable[..., Awaitable[Union[dict, None]]] # Coroutine qui retourne un dict ou None
Callable[..., Union[dict, None]],
Callable[..., Awaitable[Union[dict, None]]]
)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -137,7 +134,7 @@ class Device(object): ...@@ -137,7 +134,7 @@ class Device(object):
self.next_alive = 0 self.next_alive = 0
# Default attributes & methods # Default attributes & methods
self.__attributes = Attributes() self.__attributes = Attributes()
self.methods = { self.methods: dict[str, Union[SyncMethodT, AsyncMethodT]] = {
'get_attributes': self._get_attributes, 'get_attributes': self._get_attributes,
'get_description': self._get_description, 'get_description': self._get_description,
} }
...@@ -222,14 +219,14 @@ class Device(object): ...@@ -222,14 +219,14 @@ class Device(object):
else: else:
raise DeviceError("Invalid attributes list, use class Attributes)") raise DeviceError("Invalid attributes list, use class Attributes)")
def add_method(self, name: str, func: MethodT): def add_method(self, name: str, func: Union[SyncMethodT, AsyncMethodT]):
self.methods.update({name: func}) self.methods.update({name: func})
def del_method(self, name: str): def del_method(self, name: str):
if name in self.methods: if name in self.methods:
del self.methods[name] del self.methods[name]
def get_methods(self) -> dict: def get_methods(self) -> dict[str, Union[SyncMethodT, AsyncMethodT]]:
return self.methods return self.methods
def update_alive(self): def update_alive(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment