From 49450c3e6111b418a356c7aded8e7330228668fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Sun, 17 May 2026 18:13:49 +0200 Subject: [PATCH] Clarify ODVariable.data_type cannot be None. Use a simple integer zero as dummy value for "unset" in instances. This fixes some type checker errors. Add type hints for functions involving those values. --- canopen/objectdictionary/__init__.py | 4 ++-- canopen/objectdictionary/eds.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index c8de5aa4..139bdc1c 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -366,7 +366,7 @@ def __init__(self, name: str, index: int, subindex: int = 0): #: The value of this variable stored in the object dictionary self.value: Optional[int] = None #: Data type according to the standard as an :class:`int` - self.data_type: Optional[int] = None + self.data_type: int = 0 #: Access type, should be "rw", "ro", "wo", or "const" self.access_type: str = "rw" #: The variable represents a DOMAIN ObjectType @@ -480,7 +480,7 @@ def encode_raw(self, value: Union[int, float, str, bytes, bytearray]) -> bytes: return self.STRUCT_TYPES[self.data_type].pack(value) except struct.error: raise ValueError("Value does not fit in specified type") - elif self.data_type is None: + elif not self.data_type: raise ObjectDictionaryError("Data type has not been specified") else: raise TypeError( diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index d47a3019..1c3c1223 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -4,7 +4,7 @@ import logging import re from configparser import NoOptionError, NoSectionError, RawConfigParser -from typing import TYPE_CHECKING +from typing import Any, TYPE_CHECKING from canopen.objectdictionary import ( ODArray, @@ -204,7 +204,7 @@ def import_from_node(node_id: int, network: canopen.network.Network): return od -def _calc_bit_length(data_type): +def _calc_bit_length(data_type: int) -> int: if data_type == datatypes.INTEGER8: return 8 elif data_type == datatypes.INTEGER16: @@ -215,7 +215,7 @@ def _calc_bit_length(data_type): return 64 else: raise ValueError( - f"Invalid data_type '{data_type}', expecting a signed integer data_type." + f"Invalid data_type 0x{data_type:04X}, expecting an integer data_type." ) @@ -229,7 +229,7 @@ def _signed_int_from_hex(hex_str, bit_length): return number -def _convert_variable(node_id, var_type, value): +def _convert_variable(node_id: int, var_type: int, value: Any) -> Any: if var_type in (datatypes.OCTET_STRING, datatypes.DOMAIN): return bytes.fromhex(value) elif var_type in (datatypes.VISIBLE_STRING, datatypes.UNICODE_STRING): @@ -245,7 +245,7 @@ def _convert_variable(node_id, var_type, value): return int(value, 0) -def _revert_variable(var_type, value): +def _revert_variable(var_type: int, value: Any) -> Any: if value is None: return None if var_type in (datatypes.OCTET_STRING, datatypes.DOMAIN):