diff --git a/libs/lib/xaal/lib/core.py b/libs/lib/xaal/lib/core.py index 07caaaa4c78e25003b105d14359945750a4195c4..716ecda6cc3c74d8b6b2519309890f313a25a5d5 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 59689954e40a322c7a8ad889326fda3fe11610bb..e4c395394ad9335ccf81dce59560765bb91816e9 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):