Skip to content

fix typing in add_listener and remove_listener #1335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions can/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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
from can.message import Message

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

MessageRecipient = Union[Listener, Callable[[Message], None]]
MessageRecipient = Union[Listener, Callable[[Message], Union[Awaitable[None], None]]]


class Notifier:
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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.
Expand Down