Skip to content

Commit

Permalink
Fix some type checker errors in network module (#534)
Browse files Browse the repository at this point in the history
* Annotate PeriodicMessageTask in NmtMaster and NmtSlave.

* Type annotate Network.notifier.

* 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.
  • Loading branch information
acolomb committed Aug 14, 2024
1 parent 6ef4f5e commit 1b7c06f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 1 addition & 2 deletions canopen/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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()
Expand Down Expand Up @@ -311,7 +311,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):
Expand Down
10 changes: 7 additions & 3 deletions canopen/nmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1b7c06f

Please sign in to comment.