11"""
22Object Dictionary module
33"""
4+ from __future__ import annotations
5+
46import struct
5- from typing import Dict , Iterable , List , Optional , TextIO , Union
7+ from typing import Dict , Iterator , List , Optional , TextIO , Union
68from collections .abc import MutableMapping , Mapping
79import logging
810
1315logger = logging .getLogger (__name__ )
1416
1517
16- def export_od (od , dest :Union [str ,TextIO ,None ]= None , doc_type :Optional [str ]= None ):
18+ def export_od (od , dest : Union [str , TextIO , None ] = None , doc_type : Optional [str ] = None ):
1719 """ Export :class: ObjectDictionary to a file.
1820
1921 :param od:
@@ -55,7 +57,7 @@ def export_od(od, dest:Union[str,TextIO,None]=None, doc_type:Optional[str]=None)
5557def import_od (
5658 source : Union [str , TextIO , None ],
5759 node_id : Optional [int ] = None ,
58- ) -> " ObjectDictionary" :
60+ ) -> ObjectDictionary :
5961 """Parse an EDS, DCF, or EPF file.
6062
6163 :param source:
@@ -102,7 +104,7 @@ def __init__(self):
102104
103105 def __getitem__ (
104106 self , index : Union [int , str ]
105- ) -> Union [" ODArray" , " ODRecord" , " ODVariable" ]:
107+ ) -> Union [ODArray , ODRecord , ODVariable ]:
106108 """Get object from object dictionary by name or index."""
107109 item = self .names .get (index ) or self .indices .get (index )
108110 if item is None :
@@ -113,7 +115,7 @@ def __getitem__(
113115 return item
114116
115117 def __setitem__ (
116- self , index : Union [int , str ], obj : Union [" ODArray" , " ODRecord" , " ODVariable" ]
118+ self , index : Union [int , str ], obj : Union [ODArray , ODRecord , ODVariable ]
117119 ):
118120 assert index == obj .index or index == obj .name
119121 self .add_object (obj )
@@ -123,7 +125,7 @@ def __delitem__(self, index: Union[int, str]):
123125 del self .indices [obj .index ]
124126 del self .names [obj .name ]
125127
126- def __iter__ (self ) -> Iterable [int ]:
128+ def __iter__ (self ) -> Iterator [int ]:
127129 return iter (sorted (self .indices ))
128130
129131 def __len__ (self ) -> int :
@@ -132,7 +134,7 @@ def __len__(self) -> int:
132134 def __contains__ (self , index : Union [int , str ]):
133135 return index in self .names or index in self .indices
134136
135- def add_object (self , obj : Union [" ODArray" , " ODRecord" , " ODVariable" ]) -> None :
137+ def add_object (self , obj : Union [ODArray , ODRecord , ODVariable ]) -> None :
136138 """Add object to the object dictionary.
137139
138140 :param obj:
@@ -147,7 +149,7 @@ def add_object(self, obj: Union["ODArray", "ODRecord", "ODVariable"]) -> None:
147149
148150 def get_variable (
149151 self , index : Union [int , str ], subindex : int = 0
150- ) -> Optional [" ODVariable" ]:
152+ ) -> Optional [ODVariable ]:
151153 """Get the variable object at specified index (and subindex if applicable).
152154
153155 :return: ODVariable if found, else `None`
@@ -182,13 +184,13 @@ def __init__(self, name: str, index: int):
182184 def __repr__ (self ) -> str :
183185 return f"<{ type (self ).__qualname__ } { self .name !r} at { pretty_index (self .index )} >"
184186
185- def __getitem__ (self , subindex : Union [int , str ]) -> " ODVariable" :
187+ def __getitem__ (self , subindex : Union [int , str ]) -> ODVariable :
186188 item = self .names .get (subindex ) or self .subindices .get (subindex )
187189 if item is None :
188190 raise KeyError (f"Subindex { pretty_index (None , subindex )} was not found" )
189191 return item
190192
191- def __setitem__ (self , subindex : Union [int , str ], var : " ODVariable" ):
193+ def __setitem__ (self , subindex : Union [int , str ], var : ODVariable ):
192194 assert subindex == var .subindex
193195 self .add_member (var )
194196
@@ -200,16 +202,16 @@ def __delitem__(self, subindex: Union[int, str]):
200202 def __len__ (self ) -> int :
201203 return len (self .subindices )
202204
203- def __iter__ (self ) -> Iterable [int ]:
205+ def __iter__ (self ) -> Iterator [int ]:
204206 return iter (sorted (self .subindices ))
205207
206208 def __contains__ (self , subindex : Union [int , str ]) -> bool :
207209 return subindex in self .names or subindex in self .subindices
208210
209- def __eq__ (self , other : " ODRecord" ) -> bool :
211+ def __eq__ (self , other : ODRecord ) -> bool :
210212 return self .index == other .index
211213
212- def add_member (self , variable : " ODVariable" ) -> None :
214+ def add_member (self , variable : ODVariable ) -> None :
213215 """Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
214216 variable .parent = self
215217 self .subindices [variable .subindex ] = variable
@@ -241,7 +243,7 @@ def __init__(self, name: str, index: int):
241243 def __repr__ (self ) -> str :
242244 return f"<{ type (self ).__qualname__ } { self .name !r} at { pretty_index (self .index )} >"
243245
244- def __getitem__ (self , subindex : Union [int , str ]) -> " ODVariable" :
246+ def __getitem__ (self , subindex : Union [int , str ]) -> ODVariable :
245247 var = self .names .get (subindex ) or self .subindices .get (subindex )
246248 if var is not None :
247249 # This subindex is defined
@@ -264,13 +266,13 @@ def __getitem__(self, subindex: Union[int, str]) -> "ODVariable":
264266 def __len__ (self ) -> int :
265267 return len (self .subindices )
266268
267- def __iter__ (self ) -> Iterable [int ]:
269+ def __iter__ (self ) -> Iterator [int ]:
268270 return iter (sorted (self .subindices ))
269271
270- def __eq__ (self , other : " ODArray" ) -> bool :
272+ def __eq__ (self , other : ODArray ) -> bool :
271273 return self .index == other .index
272274
273- def add_member (self , variable : " ODVariable" ) -> None :
275+ def add_member (self , variable : ODVariable ) -> None :
274276 """Adds a :class:`~canopen.objectdictionary.ODVariable` to the record."""
275277 variable .parent = self
276278 self .subindices [variable .subindex ] = variable
@@ -348,7 +350,7 @@ def qualname(self) -> str:
348350 return f"{ self .parent .name } .{ self .name } "
349351 return self .name
350352
351- def __eq__ (self , other : " ODVariable" ) -> bool :
353+ def __eq__ (self , other : ODVariable ) -> bool :
352354 return (self .index == other .index and
353355 self .subindex == other .subindex )
354356
0 commit comments