From ea17a52388268ed0740687452aed7e553023e19a Mon Sep 17 00:00:00 2001 From: Pierre-Luc Tessier Gagne Date: Wed, 3 Jun 2026 08:48:28 -0400 Subject: [PATCH] refactor(notifier): change listeners from list to set for uniqueness This prevents adding the same listener multiple times --- can/notifier.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/can/notifier.py b/can/notifier.py index cb91cf7b4..fd21a0662 100644 --- a/can/notifier.py +++ b/can/notifier.py @@ -133,7 +133,7 @@ def __init__( :raises ValueError: If a passed in *bus* is already assigned to an active :class:`~can.Notifier`. """ - self.listeners: list[MessageRecipient] = list(listeners) + self.listeners: set[MessageRecipient] = set(listeners) self._bus_list: list[BusABC] = [] self.timeout = timeout self._loop = loop @@ -278,20 +278,18 @@ def _on_error(self, exc: Exception) -> bool: return was_handled 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. + """Add new Listener to the notification set. :param listener: Listener to be added to the list to be notified """ - self.listeners.append(listener) + self.listeners.add(listener) def remove_listener(self, listener: MessageRecipient) -> None: - """Remove a listener from the notification list. This method + """Remove a listener from the notification set. This method throws an exception if the given listener is not part of the stored listeners. - :param listener: Listener to be removed from the list to be notified + :param listener: Listener to be removed from the set to be notified :raises ValueError: if `listener` was never added to this notifier """ self.listeners.remove(listener)