@@ -69,7 +69,7 @@ def export_od(
6969 finally :
7070 # If dest is opened in this fn, it should be closed
7171 if opened_here :
72- dest .close ()
72+ dest .close () # type: ignore[union-attr]
7373
7474
7575def import_od (
@@ -92,7 +92,7 @@ def import_od(
9292 return ObjectDictionary ()
9393 if hasattr (source , "read" ):
9494 # File like object
95- filename = source .name
95+ filename = source .name # type: ignore[union-attr]
9696 elif hasattr (source , "tag" ):
9797 # XML tree, probably from an EPF file
9898 filename = "od.epf"
@@ -139,7 +139,10 @@ def __getitem__(
139139 if item is None :
140140 if isinstance (index , str ) and '.' in index :
141141 idx , sub = index .split ('.' , maxsplit = 1 )
142- return self [idx ][sub ]
142+ parent = self [idx ]
143+ if not isinstance (parent , (ODRecord , ODArray )):
144+ raise KeyError (f"{ pretty_index (index )} was not found in Object Dictionary" )
145+ return parent [sub ]
143146 raise KeyError (f"{ pretty_index (index )} was not found in Object Dictionary" )
144147 return item
145148
@@ -188,6 +191,7 @@ def get_variable(
188191 return obj
189192 elif isinstance (obj , (ODRecord , ODArray )):
190193 return obj .get (subindex )
194+ return None
191195
192196
193197class ODRecord (MutableMapping ):
@@ -259,7 +263,7 @@ class ODArray(Mapping):
259263
260264 def __init__ (self , name : str , index : int ):
261265 #: The :class:`~canopen.ObjectDictionary` owning the record.
262- self .parent = None
266+ self .parent : Optional [ ObjectDictionary ] = None
263267 #: 16-bit address of the array
264268 self .index = index
265269 #: Name of array
@@ -339,7 +343,7 @@ def __init__(self, name: str, index: int, subindex: int = 0):
339343 #: The :class:`~canopen.ObjectDictionary`,
340344 #: :class:`~canopen.objectdictionary.ODRecord` or
341345 #: :class:`~canopen.objectdictionary.ODArray` owning the variable
342- self .parent = None
346+ self .parent : Union [ ObjectDictionary , ODRecord , ODArray , None ] = None
343347 #: 16-bit address of the object in the dictionary
344348 self .index = index
345349 #: 8-bit sub-index of the object in the dictionary
@@ -451,19 +455,19 @@ def encode_raw(self, value: Union[int, float, str, bytes, bytearray]) -> bytes:
451455 if isinstance (value , (bytes , bytearray )):
452456 return value
453457 elif self .data_type == VISIBLE_STRING :
454- return value .encode ("ascii" )
458+ return value .encode ("ascii" ) # type: ignore[union-attr]
455459 elif self .data_type == UNICODE_STRING :
456- return value .encode ("utf_16_le" )
460+ return value .encode ("utf_16_le" ) # type: ignore[union-attr]
457461 elif self .data_type in (DOMAIN , OCTET_STRING ):
458- return bytes (value )
462+ return bytes (value ) # type: ignore[arg-type]
459463 elif self .data_type in self .STRUCT_TYPES :
460464 if self .data_type in INTEGER_TYPES :
461465 value = int (value )
462466 if self .data_type in NUMBER_TYPES :
463- if self .min is not None and value < self .min :
467+ if self .min is not None and value < self .min : # type: ignore[operator]
464468 logger .warning (
465469 "Value %d is less than min value %d" , value , self .min )
466- if self .max is not None and value > self .max :
470+ if self .max is not None and value > self .max : # type: ignore[operator]
467471 logger .warning (
468472 "Value %d is greater than max value %d" ,
469473 value , self .max )
@@ -477,16 +481,15 @@ def encode_raw(self, value: Union[int, float, str, bytes, bytearray]) -> bytes:
477481 raise TypeError (
478482 f"Do not know how to encode { value !r} to data type 0x{ self .data_type :X} " )
479483
480- def decode_phys (self , value : int ) -> Union [int , bool , float , str , bytes ]:
484+ def decode_phys (self , value : int ) -> Union [int , float ]:
481485 if self .data_type in INTEGER_TYPES :
482- value *= self .factor
486+ return value * self .factor
483487 return value
484488
485489 def encode_phys (self , value : Union [int , bool , float , str , bytes ]) -> int :
486490 if self .data_type in INTEGER_TYPES :
487- value /= self .factor
488- value = int (round (value ))
489- return value
491+ value = int (round (value / self .factor )) # type: ignore[operator]
492+ return value # type: ignore[return-value]
490493
491494 def decode_desc (self , value : int ) -> str :
492495 if not self .value_descriptions :
0 commit comments