Skip to content

Commit 5900056

Browse files
committed
Add central typings to ease project navigation
1 parent c46228f commit 5900056

File tree

11 files changed

+70
-24
lines changed

11 files changed

+70
-24
lines changed

canopen/emcy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from __future__ import annotations
12
import struct
23
import logging
34
import threading
45
import time
5-
from typing import Callable, List, Optional
6+
from typing import Callable, List, Optional, TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from .network import Network
610

711
# Error code, error register, vendor specific data
812
EMCY_STRUCT = struct.Struct("<HB5s")
@@ -82,7 +86,7 @@ def wait(
8286
class EmcyProducer(object):
8387

8488
def __init__(self, cob_id: int):
85-
self.network = None
89+
self.network: Optional[Network] = None
8690
self.cob_id = cob_id
8791

8892
def send(self, code: int, register: int = 0, data: bytes = b""):

canopen/lss.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
from typing import Optional, TYPE_CHECKING
13
import logging
24
import time
35
import struct
@@ -6,6 +8,9 @@
68
except ImportError:
79
import Queue as queue
810

11+
if TYPE_CHECKING:
12+
from .network import Network
13+
914
logger = logging.getLogger(__name__)
1015

1116
# Command Specifier (CS)
@@ -82,7 +87,7 @@ class LssMaster(object):
8287
RESPONSE_TIMEOUT = 0.5
8388

8489
def __init__(self):
85-
self.network = None
90+
self.network: Optional[Network] = None
8691
self._node_id = 0
8792
self._data = None
8893
self.responses = queue.Queue()

canopen/network.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from __future__ import annotations
12
try:
23
from collections.abc import MutableMapping
34
except ImportError:
45
from collections import MutableMapping
56
import logging
67
import threading
7-
from typing import Callable, Dict, Iterable, List, Optional, Union
8+
from typing import Callable, Dict, Iterable, List, Optional, Union, TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from can import BusABC, Notifier
812

913
try:
1014
import can
@@ -39,13 +43,13 @@ def __init__(self, bus=None):
3943
"""
4044
#: A python-can :class:`can.BusABC` instance which is set after
4145
#: :meth:`canopen.Network.connect` is called
42-
self.bus = bus
46+
self.bus: Optional[BusABC] = bus
4347
#: A :class:`~canopen.network.NodeScanner` for detecting nodes
4448
self.scanner = NodeScanner(self)
4549
#: List of :class:`can.Listener` objects.
4650
#: Includes at least MessageListener.
4751
self.listeners = [MessageListener(self)]
48-
self.notifier = None
52+
self.notifier: Optional[Notifier] = None
4953
self.nodes: Dict[int, Union[RemoteNode, LocalNode]] = {}
5054
self.subscribers: Dict[int, List[Callback]] = {}
5155
self.send_lock = threading.Lock()

canopen/nmt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
from __future__ import annotations
12
import threading
23
import logging
34
import struct
45
import time
5-
from typing import Callable, Optional
6+
from typing import Callable, Optional, TYPE_CHECKING
67

78
from .network import CanError
89

10+
if TYPE_CHECKING:
11+
from .network import Network
12+
13+
914
logger = logging.getLogger(__name__)
1015

1116
NMT_STATES = {
@@ -47,7 +52,7 @@ class NmtBase(object):
4752

4853
def __init__(self, node_id: int):
4954
self.id = node_id
50-
self.network = None
55+
self.network: Optional[Network] = None
5156
self._state = 0
5257

5358
def on_command(self, can_id, data, timestamp):

canopen/node/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
from typing import TextIO, Union
1+
from __future__ import annotations
2+
from typing import TextIO, Union, Optional, TYPE_CHECKING
23
from .. import objectdictionary
34

5+
if TYPE_CHECKING:
6+
from ..network import Network
7+
48

59
class BaseNode(object):
610
"""A CANopen node.
@@ -17,7 +21,7 @@ def __init__(
1721
node_id: int,
1822
object_dictionary: Union[objectdictionary.ObjectDictionary, str, TextIO],
1923
):
20-
self.network = None
24+
self.network: Optional[Network] = None
2125

2226
if not isinstance(object_dictionary,
2327
objectdictionary.ObjectDictionary):

canopen/node/local.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
12
import logging
2-
from typing import Dict, Union
3+
from typing import Dict, Union, TYPE_CHECKING
34

45
from .base import BaseNode
56
from ..sdo import SdoServer, SdoAbortedError
@@ -8,6 +9,9 @@
89
from ..emcy import EmcyProducer
910
from .. import objectdictionary
1011

12+
if TYPE_CHECKING:
13+
from ..network import Network
14+
1115
logger = logging.getLogger(__name__)
1216

1317

@@ -33,7 +37,7 @@ def __init__(
3337
self.add_write_callback(self.nmt.on_write)
3438
self.emcy = EmcyProducer(0x80 + self.id)
3539

36-
def associate_network(self, network):
40+
def associate_network(self, network: Network):
3741
self.network = network
3842
self.sdo.network = network
3943
self.tpdo.network = network

canopen/node/remote.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
12
import logging
2-
from typing import Union, TextIO
3+
from typing import Union, TextIO, TYPE_CHECKING
34

45
from ..sdo import SdoClient
56
from ..nmt import NmtMaster
@@ -12,6 +13,9 @@
1213

1314
from canopen import objectdictionary
1415

16+
if TYPE_CHECKING:
17+
from ..network import Network
18+
1519
logger = logging.getLogger(__name__)
1620

1721

@@ -50,7 +54,7 @@ def __init__(
5054
if load_od:
5155
self.load_configuration()
5256

53-
def associate_network(self, network):
57+
def associate_network(self, network: Network):
5458
self.network = network
5559
self.sdo.network = network
5660
self.pdo.network = network

canopen/pdo/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
from __future__ import annotations
12
import threading
23
import math
3-
from typing import Callable, Dict, Iterable, List, Optional, Union
4+
from typing import Callable, Dict, Iterable, List, Optional, Union, TYPE_CHECKING
45
try:
56
from collections.abc import Mapping
67
except ImportError:
78
from collections import Mapping
89
import logging
910
import binascii
1011

12+
if TYPE_CHECKING:
13+
from ..network import Network
14+
1115
from ..sdo import SdoAbortedError
1216
from .. import objectdictionary
1317
from .. import variable
@@ -26,14 +30,14 @@ class PdoBase(Mapping):
2630
"""
2731

2832
def __init__(self, node):
29-
self.network = None
33+
self.network: Optional[Network] = None
3034
self.map = None # instance of Maps
3135
self.node = node
3236

3337
def __iter__(self):
3438
return iter(self.map)
3539

36-
def __getitem__(self, key):
40+
def __getitem__(self, key) -> PdoBase:
3741
if isinstance(key, int) and (0x1A00 <= key <= 0x1BFF or # By TPDO ID (512)
3842
0x1600 <= key <= 0x17FF or # By RPDO ID (512)
3943
0 < key <= 512): # By PDO Index
@@ -160,7 +164,7 @@ def __len__(self) -> int:
160164
class Map(object):
161165
"""One message which can have up to 8 bytes of variables mapped."""
162166

163-
def __init__(self, pdo_node, com_record, map_array):
167+
def __init__(self, pdo_node: PdoBase, com_record, map_array):
164168
self.pdo_node = pdo_node
165169
self.com_record = com_record
166170
self.map_array = map_array

canopen/sdo/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
12
import binascii
2-
from typing import Iterable, Union
3+
from typing import Iterable, Union, Optional, TYPE_CHECKING
34
try:
45
from collections.abc import Mapping
56
except ImportError:
@@ -8,6 +9,9 @@
89
from .. import objectdictionary
910
from .. import variable
1011

12+
if TYPE_CHECKING:
13+
from ..network import Network
14+
1115

1216
class CrcXmodem(object):
1317
"""Mimics CrcXmodem from crccheck."""
@@ -43,7 +47,7 @@ def __init__(
4347
"""
4448
self.rx_cobid = rx_cobid
4549
self.tx_cobid = tx_cobid
46-
self.network = None
50+
self.network: Optional[Network] = None
4751
self.od = od
4852

4953
def __getitem__(

canopen/sync.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from __future__ import annotations
12

3+
from typing import Optional, TYPE_CHECKING
24

3-
from typing import Optional
5+
if TYPE_CHECKING:
6+
from .network import Network
47

58

69
class SyncProducer(object):
@@ -9,7 +12,7 @@ class SyncProducer(object):
912
#: COB-ID of the SYNC message
1013
cob_id = 0x80
1114

12-
def __init__(self, network):
15+
def __init__(self, network: Network):
1316
self.network = network
1417
self.period: Optional[float] = None
1518
self._task = None

0 commit comments

Comments
 (0)