Skip to content

Commit bc917f0

Browse files
sveinseacolomb
andauthored
Type annotate everywhere Network is used (#575)
Co-authored-by: André Colomb <src@andre.colomb.de>
1 parent cf9f33e commit bc917f0

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

canopen/network.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ class NodeScanner:
392392
SERVICES = (0x700, 0x580, 0x180, 0x280, 0x380, 0x480, 0x80)
393393

394394
def __init__(self, network: Optional[Network] = None):
395-
self.network = network
395+
if network is None:
396+
network = _UNINITIALIZED_NETWORK
397+
self.network: Network = network
396398
#: A :class:`list` of nodes discovered
397399
self.nodes: List[int] = []
398400

@@ -408,8 +410,6 @@ def reset(self):
408410

409411
def search(self, limit: int = 127) -> None:
410412
"""Search for nodes by sending SDO requests to all node IDs."""
411-
if self.network is None:
412-
raise RuntimeError("A Network is required to do active scanning")
413413
sdo_req = b"\x40\x00\x10\x00\x00\x00\x00\x00"
414414
for node_id in range(1, limit + 1):
415415
self.network.send_message(0x600 + node_id, sdo_req)

canopen/objectdictionary/eds.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
from __future__ import annotations
2+
13
import copy
24
import logging
35
import re
46
from configparser import NoOptionError, NoSectionError, RawConfigParser
7+
from typing import TYPE_CHECKING
58

69
from canopen import objectdictionary
710
from canopen.objectdictionary import ObjectDictionary, datatypes
811
from canopen.sdo import SdoClient
912

13+
if TYPE_CHECKING:
14+
import canopen.network
15+
1016

1117
logger = logging.getLogger(__name__)
1218

@@ -173,7 +179,7 @@ def import_eds(source, node_id):
173179
return od
174180

175181

176-
def import_from_node(node_id, network):
182+
def import_from_node(node_id: int, network: canopen.network.Network):
177183
""" Download the configuration from the remote node
178184
:param int node_id: Identifier of the node
179185
:param network: network object

canopen/sync.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from typing import Optional
1+
from __future__ import annotations
2+
3+
from typing import Optional, TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
import canopen.network
27

38

49
class SyncProducer:
@@ -7,7 +12,7 @@ class SyncProducer:
712
#: COB-ID of the SYNC message
813
cob_id = 0x80
914

10-
def __init__(self, network):
15+
def __init__(self, network: canopen.network.Network):
1116
self.network = network
1217
self.period: Optional[float] = None
1318
self._task = None

canopen/timestamp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
from __future__ import annotations
2+
13
import struct
24
import time
3-
from typing import Optional
5+
from typing import Optional, TYPE_CHECKING
6+
7+
if TYPE_CHECKING:
8+
import canopen.network
49

510

611
# 1 Jan 1984
@@ -17,7 +22,7 @@ class TimeProducer:
1722
#: COB-ID of the SYNC message
1823
cob_id = 0x100
1924

20-
def __init__(self, network):
25+
def __init__(self, network: canopen.network.Network):
2126
self.network = network
2227

2328
def transmit(self, timestamp: Optional[float] = None):

test/test_network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def test_scanner_reset(self):
318318
self.assertListEqual(self.scanner.nodes, [])
319319

320320
def test_scanner_search_no_network(self):
321-
with self.assertRaisesRegex(RuntimeError, "Network is required"):
321+
with self.assertRaisesRegex(RuntimeError, "No actual Network object was assigned"):
322322
self.scanner.search()
323323

324324
def test_scanner_search(self):

0 commit comments

Comments
 (0)