diff --git a/libs/lib/xaal/lib/aioengine.py b/libs/lib/xaal/lib/aioengine.py index 400a23ed2dcb5a6939470e0da4234e0269eff0ef..f4f3c2bf75b58171dd0045115a7e3f7194b814de 100644 --- a/libs/lib/xaal/lib/aioengine.py +++ b/libs/lib/xaal/lib/aioengine.py @@ -36,7 +36,7 @@ class HookType(Enum): class Hook(object): __slots__ = ['type', 'func', 'args', 'kwargs'] - def __init__(self, type_:HookType, func: Any, *args, **kwargs): + def __init__(self, type_:HookType, func: core.FuncT, *args, **kwargs): # func has to be a callable, but it can be a coroutine or a function self.type = type_ self.func = func @@ -83,11 +83,11 @@ class AsyncEngine(core.EngineMixin): ##################################################### # Hooks ##################################################### - def on_start(self, func: Any, *args, **kwargs): + def on_start(self, func: core.FuncT, *args, **kwargs): hook = Hook(HookType.start, func, *args, **kwargs) self._hooks.append(hook) - def on_stop(self, func: Any, *args, **kwargs): + def on_stop(self, func: core.FuncT, *args, **kwargs): hook = Hook(HookType.stop, func, *args, **kwargs) self._hooks.append(hook) diff --git a/libs/lib/xaal/lib/core.py b/libs/lib/xaal/lib/core.py index ca2d975c8f37f3b71c310a5402cf10f1cebc2db8..07caaaa4c78e25003b105d14359945750a4195c4 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, Optional, List, Callable +from typing import Any, Awaitable, Optional, List, Callable, Union, TypeVar from .exceptions import EngineError, XAALError from .messages import ALIVE_ADDR, MessageAction, MessageFactory, MessageType @@ -32,6 +32,7 @@ if typing.TYPE_CHECKING: from .messages import Message +FuncT = TypeVar("FuncT", Callable[[], None], Callable[[], Awaitable[None]]) logger = logging.getLogger(__name__) @@ -39,7 +40,7 @@ logger = logging.getLogger(__name__) # Timer class ##################################################### class Timer(object): - def __init__(self, func: Any, period: int, counter: int): + def __init__(self, func: FuncT, period: int, counter: int): # Timer function should a Callable[[],None], but it can be a coroutine too self.func = func self.period = period @@ -208,7 +209,8 @@ class EngineMixin(object): ##################################################### # timers ##################################################### - def add_timer(self, func: Any, period: int, counter: int = -1): + from typing import Coroutine + def add_timer(self, func: FuncT, period: int, counter: int = -1): """ func: function to call period: period in second