Skip to content

Commit 01429c1

Browse files
committed
Fix trivial mypy errors
- Add mypy overrides to ignore missing imports for 'can' and 'canmatrix' - Replace List[int] with list[int] (PEP 585, supported since Python 3.9) - Add type annotations for untyped variables (var-annotated): - lss.py: Queue[bytes] - node/local.py: list[Callable] for callbacks - node/remote.py: list[SdoClient] for sdo_channels - objectdictionary/__init__.py: dict[int, ODVariable] / dict[str, ODVariable]
1 parent 4e789fe commit 01429c1

5 files changed

Lines changed: 19 additions & 11 deletions

File tree

canopen/lss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __init__(self) -> None:
8585
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
8686
self._node_id = 0
8787
self._data = None
88-
self.responses = queue.Queue()
88+
self.responses: queue.Queue[bytes] = queue.Queue()
8989

9090
def send_switch_state_global(self, mode):
9191
"""switch mode to CONFIGURATION_STATE or WAITING_STATE

canopen/node/local.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
from collections.abc import Callable
45
from typing import Union
56

67
import canopen.network
@@ -26,8 +27,8 @@ def __init__(
2627
super(LocalNode, self).__init__(node_id, object_dictionary)
2728

2829
self.data_store: dict[int, dict[int, bytes]] = {}
29-
self._read_callbacks = []
30-
self._write_callbacks = []
30+
self._read_callbacks: list[Callable] = []
31+
self._write_callbacks: list[Callable] = []
3132

3233
self.sdo = SdoServer(0x600 + self.id, 0x580 + self.id, self)
3334
self.tpdo = TPDO(self)

canopen/node/remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(
3939
#: Enable WORKAROUND for reversed PDO mapping entries
4040
self.curtis_hack = False
4141

42-
self.sdo_channels = []
42+
self.sdo_channels: list[SdoClient] = []
4343
self.sdo = self.add_sdo(0x600 + self.id, 0x580 + self.id)
4444
self.tpdo = TPDO(self)
4545
self.rpdo = RPDO(self)

canopen/objectdictionary/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def __init__(self, name: str, index: int):
207207
self.name = name
208208
#: Storage location of index
209209
self.storage_location = None
210-
self.subindices = {}
211-
self.names = {}
210+
self.subindices: dict[int, ODVariable] = {}
211+
self.names: dict[str, ODVariable] = {}
212212

213213
def __repr__(self) -> str:
214214
return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>"
@@ -266,8 +266,8 @@ def __init__(self, name: str, index: int):
266266
self.name = name
267267
#: Storage location of index
268268
self.storage_location = None
269-
self.subindices = {}
270-
self.names = {}
269+
self.subindices: dict[int, ODVariable] = {}
270+
self.names: dict[str, ODVariable] = {}
271271

272272
def __repr__(self) -> str:
273273
return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>"
@@ -413,7 +413,7 @@ def add_value_description(self, value: int, descr: str) -> None:
413413
"""
414414
self.value_descriptions[value] = descr
415415

416-
def add_bit_definition(self, name: str, bits: List[int]) -> None:
416+
def add_bit_definition(self, name: str, bits: list[int]) -> None:
417417
"""Associate bit(s) with a string description.
418418
419419
:param name: Name of bit(s)
@@ -508,7 +508,7 @@ def encode_desc(self, desc: str) -> int:
508508
raise ValueError(
509509
f"No value corresponds to '{desc}'. Valid values are: {valid_values}")
510510

511-
def decode_bits(self, value: int, bits: List[int]) -> int:
511+
def decode_bits(self, value: int, bits: list[int]) -> int:
512512
try:
513513
bits = self.bit_definitions[bits]
514514
except (TypeError, KeyError):
@@ -518,7 +518,7 @@ def decode_bits(self, value: int, bits: List[int]) -> int:
518518
mask |= 1 << bit
519519
return (value & mask) >> min(bits)
520520

521-
def encode_bits(self, original_value: int, bits: List[int], bit_value: int):
521+
def encode_bits(self, original_value: int, bits: list[int], bit_value: int):
522522
try:
523523
bits = self.bit_definitions[bits]
524524
except (TypeError, KeyError):

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ exclude = [
5656
"^test*",
5757
"^setup.py*",
5858
]
59+
60+
[[tool.mypy.overrides]]
61+
module = [
62+
"can.*",
63+
"canmatrix.*",
64+
]
65+
ignore_missing_imports = true

0 commit comments

Comments
 (0)