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

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.
parent 386afa4c
No related branches found
No related tags found
1 merge request!1First try of type hints
......@@ -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)
#####################################################
......
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment