From d87b865e22ae597c2dcb57406c173f4c9042dfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Mon, 12 Aug 2024 16:34:17 +0200 Subject: [PATCH 1/3] Annotate PeriodicMessageTask in NmtMaster and NmtSlave. --- canopen/nmt.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/canopen/nmt.py b/canopen/nmt.py index 8ce737ea..125042d3 100644 --- a/canopen/nmt.py +++ b/canopen/nmt.py @@ -2,7 +2,11 @@ import logging import struct import time -from typing import Callable, Optional +from typing import Callable, Optional, TYPE_CHECKING + +if TYPE_CHECKING: + from canopen.network import PeriodicMessageTask + logger = logging.getLogger(__name__) @@ -107,7 +111,7 @@ class NmtMaster(NmtBase): def __init__(self, node_id: int): super(NmtMaster, self).__init__(node_id) self._state_received = None - self._node_guarding_producer = None + self._node_guarding_producer: Optional[PeriodicMessageTask] = None #: Timestamp of last heartbeat message self.timestamp: Optional[float] = None self.state_update = threading.Condition() @@ -197,7 +201,7 @@ class NmtSlave(NmtBase): def __init__(self, node_id: int, local_node): super(NmtSlave, self).__init__(node_id) - self._send_task = None + self._send_task: Optional[PeriodicMessageTask] = None self._heartbeat_time_ms = 0 self._local_node = local_node From c82f646a02784c791775b2b50c0f6eba899d2088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Mon, 12 Aug 2024 16:35:32 +0200 Subject: [PATCH 2/3] Type annotate Network.notifier. --- canopen/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canopen/network.py b/canopen/network.py index e886b655..e62f3e03 100644 --- a/canopen/network.py +++ b/canopen/network.py @@ -38,7 +38,7 @@ def __init__(self, bus: Optional[can.BusABC] = None): #: List of :class:`can.Listener` objects. #: Includes at least MessageListener. self.listeners = [MessageListener(self)] - self.notifier = None + self.notifier: Optional[can.Notifier] = None self.nodes: Dict[int, Union[RemoteNode, LocalNode]] = {} self.subscribers: Dict[int, List[Callback]] = {} self.send_lock = threading.Lock() From b8187cda7baaad7ad49155ff69fbb98e69ba3d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Mon, 12 Aug 2024 16:35:47 +0200 Subject: [PATCH 3/3] Remove duplicate initialization of PeriodicMessageTask._task. The attribute is set within the method directly following. So there is no need to initialize it to None before, only to confuse type checkers. --- canopen/network.py | 1 - 1 file changed, 1 deletion(-) diff --git a/canopen/network.py b/canopen/network.py index e62f3e03..1e52d9c7 100644 --- a/canopen/network.py +++ b/canopen/network.py @@ -308,7 +308,6 @@ def __init__( self.msg = can.Message(is_extended_id=can_id > 0x7FF, arbitration_id=can_id, data=data, is_remote_frame=remote) - self._task = None self._start() def _start(self):