From 888890adf1992ec0c9a8448db1605a44f6cd727b Mon Sep 17 00:00:00 2001 From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr> Date: Wed, 27 Nov 2024 12:10:56 +0100 Subject: [PATCH] Fix the subscribers func type, fix method func type It's really awfull to write this kind of suff. It seems to be Ok right now. I hope I will not change it until long time. --- libs/lib/xaal/lib/core.py | 16 ++++++++++++---- libs/lib/xaal/lib/devices.py | 11 +++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/libs/lib/xaal/lib/core.py b/libs/lib/xaal/lib/core.py index 07caaaa4..716ecda6 100644 --- a/libs/lib/xaal/lib/core.py +++ b/libs/lib/xaal/lib/core.py @@ -22,7 +22,7 @@ import inspect import logging import time import typing -from typing import Any, Awaitable, Optional, List, Callable, Union, TypeVar +from typing import Any, Awaitable, Optional, List, Callable, TypeVar from .exceptions import EngineError, XAALError from .messages import ALIVE_ADDR, MessageAction, MessageFactory, MessageType @@ -31,8 +31,16 @@ if typing.TYPE_CHECKING: from .devices import Device, Attribute from .messages import Message - +# Function type w/ no argument, and no return (Timer, Hooks) FuncT = TypeVar("FuncT", Callable[[], None], Callable[[], Awaitable[None]]) + +# Function type w/ message as argument (subscribers), no return +SubFuncT = TypeVar( + "SubFuncT", + Callable[[str], None], + Callable[[str], Awaitable[None]] +) + logger = logging.getLogger(__name__) @@ -199,11 +207,11 @@ class EngineMixin(object): ##################################################### # xAAL messages subscribers ##################################################### - def subscribe(self, func: Any): + def subscribe(self, func: SubFuncT): # func should be a Callable[[Message],None], but it can be a coroutine too self.subscribers.append(func) - def unsubscribe(self, func: Any): + def unsubscribe(self, func: SubFuncT): self.subscribers.remove(func) ##################################################### diff --git a/libs/lib/xaal/lib/devices.py b/libs/lib/xaal/lib/devices.py index 59689954..e4c39539 100644 --- a/libs/lib/xaal/lib/devices.py +++ b/libs/lib/xaal/lib/devices.py @@ -21,7 +21,7 @@ import logging import time import typing -from typing import Any, Optional, Union, Callable +from typing import Any, Optional, Union, Callable, TypeVar, Awaitable from tabulate import tabulate @@ -33,6 +33,13 @@ if typing.TYPE_CHECKING: from .engine import Engine from .core import EngineMixin +# Funtion type with any arguments and return a dict or None (device methods) +MethodT = TypeVar( + "MethodT", + Callable[..., Union[dict, None]], + Callable[..., Awaitable[Union[dict, None]]] +) + logger = logging.getLogger(__name__) @@ -215,7 +222,7 @@ class Device(object): else: raise DeviceError("Invalid attributes list, use class Attributes)") - def add_method(self, name: str, func: Callable[..., Any]): + def add_method(self, name: str, func: MethodT): self.methods.update({name: func}) def del_method(self, name: str): -- GitLab