From 421cc4e4499a582a3ad38f99b1a455ff54539431 Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Tue, 21 Jun 2022 11:39:18 +0200 Subject: [PATCH 1/3] fix typing in add_listener and remove_listener Use the union to typecheck these functions --- can/notifier.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/notifier.py b/can/notifier.py index f7c004c4e..cbf7cde5d 100644 --- a/can/notifier.py +++ b/can/notifier.py @@ -166,7 +166,7 @@ def _on_error(self, exc: Exception) -> bool: return was_handled - def add_listener(self, listener: Listener) -> None: + def add_listener(self, listener: MessageRecipient) -> None: """Add new Listener to the notification list. If it is already present, it will be called two times each time a message arrives. @@ -175,7 +175,7 @@ def add_listener(self, listener: Listener) -> None: """ self.listeners.append(listener) - def remove_listener(self, listener: Listener) -> None: + def remove_listener(self, listener: MessageRecipient) -> None: """Remove a listener from the notification list. This method throws an exception if the given listener is not part of the stored listeners. From 6980507e927a29b8f61e2b0028ec398384b92aaa Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Tue, 21 Jun 2022 12:35:55 +0200 Subject: [PATCH 2/3] add awaitable to MessageRecipient definition; remove unneeded cast --- can/notifier.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/notifier.py b/can/notifier.py index cbf7cde5d..4df969115 100644 --- a/can/notifier.py +++ b/can/notifier.py @@ -14,7 +14,7 @@ logger = logging.getLogger("can.Notifier") -MessageRecipient = Union[Listener, Callable[[Message], None]] +MessageRecipient = Union[Listener, Callable[[Message], Union[Awaitable[None], None]]] class Notifier: @@ -140,7 +140,7 @@ def _on_message_available(self, bus: BusABC) -> None: def _on_message_received(self, msg: Message) -> None: for callback in self.listeners: - res = cast(Union[None, Optional[Awaitable[Any]]], callback(msg)) + res = callback(msg) if res is not None and self._loop is not None and asyncio.iscoroutine(res): # Schedule coroutine self._loop.create_task(res) From d431a3a30795ad16a63e147d659ceadde9d9661b Mon Sep 17 00:00:00 2001 From: tamenol <37591107+tamenol@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:04:00 +0200 Subject: [PATCH 3/3] remove unused imports --- can/notifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/notifier.py b/can/notifier.py index 4df969115..2adae431e 100644 --- a/can/notifier.py +++ b/can/notifier.py @@ -6,7 +6,7 @@ import logging import threading import time -from typing import Any, Callable, cast, Iterable, List, Optional, Union, Awaitable +from typing import Callable, Iterable, List, Optional, Union, Awaitable from can.bus import BusABC from can.listener import Listener