Skip to content

Commit d330522

Browse files
authored
fix typing in add_listener and remove_listener (#1335)
* fix typing in add_listener and remove_listener Use the union to typecheck these functions * add awaitable to MessageRecipient definition; remove unneeded cast * remove unused imports
1 parent ec06452 commit d330522

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

can/notifier.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import logging
77
import threading
88
import time
9-
from typing import Any, Callable, cast, Iterable, List, Optional, Union, Awaitable
9+
from typing import Callable, Iterable, List, Optional, Union, Awaitable
1010

1111
from can.bus import BusABC
1212
from can.listener import Listener
1313
from can.message import Message
1414

1515
logger = logging.getLogger("can.Notifier")
1616

17-
MessageRecipient = Union[Listener, Callable[[Message], None]]
17+
MessageRecipient = Union[Listener, Callable[[Message], Union[Awaitable[None], None]]]
1818

1919

2020
class Notifier:
@@ -140,7 +140,7 @@ def _on_message_available(self, bus: BusABC) -> None:
140140

141141
def _on_message_received(self, msg: Message) -> None:
142142
for callback in self.listeners:
143-
res = cast(Union[None, Optional[Awaitable[Any]]], callback(msg))
143+
res = callback(msg)
144144
if res is not None and self._loop is not None and asyncio.iscoroutine(res):
145145
# Schedule coroutine
146146
self._loop.create_task(res)
@@ -166,7 +166,7 @@ def _on_error(self, exc: Exception) -> bool:
166166

167167
return was_handled
168168

169-
def add_listener(self, listener: Listener) -> None:
169+
def add_listener(self, listener: MessageRecipient) -> None:
170170
"""Add new Listener to the notification list.
171171
If it is already present, it will be called two times
172172
each time a message arrives.
@@ -175,7 +175,7 @@ def add_listener(self, listener: Listener) -> None:
175175
"""
176176
self.listeners.append(listener)
177177

178-
def remove_listener(self, listener: Listener) -> None:
178+
def remove_listener(self, listener: MessageRecipient) -> None:
179179
"""Remove a listener from the notification list. This method
180180
throws an exception if the given listener is not part of the
181181
stored listeners.

0 commit comments

Comments
 (0)