Skip to content

Commit 1d6bd8d

Browse files
acolombbizfsc
andauthored
Fix some wrong type hints and mypy errors (#664)
* Fix too narrow type annotations in emcy and network. * Return None explicitly where return type expects it. * Annotate ODVariable and ODArray parent attribute as for ODRecord. Co-authored-by: Frieder Schüler <frieder.schueler@bizerba.com>
1 parent 62cf726 commit 1d6bd8d

4 files changed

Lines changed: 7 additions & 5 deletions

File tree

canopen/emcy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def reset(self):
5757

5858
def wait(
5959
self, emcy_code: Optional[int] = None, timeout: float = 10
60-
) -> EmcyError:
60+
) -> Optional[EmcyError]:
6161
"""Wait for a new EMCY to arrive.
6262
6363
:param emcy_code: EMCY code to wait for

canopen/network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def add_node(
141141
node: Union[int, RemoteNode, LocalNode],
142142
object_dictionary: Union[str, ObjectDictionary, None] = None,
143143
upload_eds: bool = False,
144-
) -> RemoteNode:
144+
) -> Union[RemoteNode, LocalNode]:
145145
"""Add a remote node to the network.
146146
147147
:param node:
@@ -167,7 +167,7 @@ def add_node(
167167

168168
def create_node(
169169
self,
170-
node: int,
170+
node: Union[int, LocalNode],
171171
object_dictionary: Union[str, ObjectDictionary, None] = None,
172172
) -> LocalNode:
173173
"""Create a local node in the network.

canopen/objectdictionary/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def get_variable(
188188
return obj
189189
elif isinstance(obj, (ODRecord, ODArray)):
190190
return obj.get(subindex)
191+
return None
191192

192193

193194
class ODRecord(MutableMapping):
@@ -261,7 +262,7 @@ class ODArray(Mapping):
261262

262263
def __init__(self, name: str, index: int):
263264
#: The :class:`~canopen.ObjectDictionary` owning the record.
264-
self.parent = None
265+
self.parent: Optional[ObjectDictionary] = None
265266
#: 16-bit address of the array
266267
self.index = index
267268
#: Name of array
@@ -343,7 +344,7 @@ def __init__(self, name: str, index: int, subindex: int = 0):
343344
#: The :class:`~canopen.ObjectDictionary`,
344345
#: :class:`~canopen.objectdictionary.ODRecord` or
345346
#: :class:`~canopen.objectdictionary.ODArray` owning the variable
346-
self.parent = None
347+
self.parent: Union[ObjectDictionary, ODRecord, ODArray, None] = None
347348
#: 16-bit address of the object in the dictionary
348349
self.index = index
349350
#: 8-bit sub-index of the object in the dictionary

canopen/sdo/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def get_variable(
7979
return obj
8080
elif isinstance(obj, (SdoRecord, SdoArray)):
8181
return obj.get(subindex)
82+
return None
8283

8384
def upload(self, index: int, subindex: int) -> bytes:
8485
raise NotImplementedError()

0 commit comments

Comments
 (0)