22
33from collections import OrderedDict
44from logging import getLogger
5+ from typing import Any , Union
56
7+ from openlcb import (
8+ emit_cast ,
9+ list_type_names ,
10+ )
611from openlcb .canbus .controlframe import ControlFrame
712from openlcb .nodeid import NodeID
813
@@ -125,13 +130,13 @@ def alias(self) -> int:
125130 def __init__ (self , * args , afterSendState = None , reservation = None ,
126131 minimumState = None ):
127132 self .afterSendState = afterSendState
128- self .encoder = NoEncoder ()
133+ self .encoder = NoEncoder () # type: Any
129134 self .reservation = reservation
130135 self .minimumState = minimumState
131136 arg1 = None
132137 arg2 = None
133138 arg3 = None
134- self ._alias = None
139+ self ._alias = None # type: Union[int, None] # TODO: type: int ?
135140 if len (args ) > 0 :
136141 arg1 = args [0 ]
137142 if len (args ) > 1 :
@@ -142,7 +147,7 @@ def __init__(self, *args, afterSendState=None, reservation=None,
142147 arg3 = bytearray ()
143148 # There are three ctor forms.
144149 # - See "Args" in class for docstring.
145- self .header = 0
150+ self .header = 0 # type: int
146151 self .direction = None # See deque for usage
147152 self .data = bytearray ()
148153 # three arguments as N_cid, nodeID, alias
@@ -153,26 +158,35 @@ def __init__(self, *args, afterSendState=None, reservation=None,
153158 # (duck typing) in this case.
154159 if len (args ) < 3 :
155160 args_error = "Expected alias after NodeID"
161+ assert isinstance (arg3 , int ), \
162+ ("Expected int alias after NodeID, got"
163+ f" { emit_cast (arg3 )} " )
156164 # cid must be 4 to 7 inclusive (100 to 111 binary)
157165 # precondition(4 <= cid && cid <= 7)
158- cid = arg1
159166 nodeID = arg2
160167 self ._alias = arg3
168+ if not isinstance (arg1 , int ):
169+ args_error = \
170+ ("Expected CID 4 to 7 if arg2 is NodeID,"
171+ f" got { emit_cast (arg1 )} " )
172+ else :
173+ cid = arg1
161174
162- nodeCode = ((nodeID .value >> ((cid - 4 )* 12 )) & 0xFFF )
163- # ^ cid-4 results in 0 to 3. *12 results in 0 to 36 bit shift (nodeID size) # noqa: E501
164- self .header = ((cid << 12 ) | nodeCode ) << 12 | (self ._alias & 0xFFF ) | 0x10_00_00_00 # noqa: E501
165- # self.data = bytearray()
175+ nodeCode = ((nodeID .value >> ((cid - 4 )* 12 )) & 0xFFF )
176+ # ^ cid-4 results in 0 to 3. *12 results in 0 to 36 bit shift (nodeID size) # noqa: E501
177+ self .header = ((cid << 12 ) | nodeCode ) << 12 | (self ._alias & 0xFFF ) | 0x10_00_00_00 # noqa: E501
178+ # self.data = bytearray()
166179
167180 # two arguments as header, data
168181 elif isinstance (arg2 , bytearray ):
169182 # TODO: decode (header?) if self._alias is necessary in this case,
170183 # otherwise is remains None!
171184 if not isinstance (arg1 , int ):
172- args_error = "Expected int(header) since 2nd argument is bytearray."
185+ args_error = \
186+ "Expected int(header) since 2nd argument is bytearray."
173187 # Types of both args are enforced by this point.
174- self .header = arg1
175- self ._alias = arg1 & 0xFFF
188+ self .header = arg1 # type: ignore
189+ self ._alias = arg1 & 0xFFF # type: ignore
176190 if self ._alias == 0 :
177191 logger .warning ("Alias is {}" .format (self ._alias ))
178192 self .data = arg2
@@ -190,7 +204,7 @@ def __init__(self, *args, afterSendState=None, reservation=None,
190204 # three arguments as control, alias, data
191205 elif isinstance (arg2 , int ):
192206 # Types of all 3 are enforced by usage (duck typing) in this case.
193- control = arg1
207+ control = arg1 # type: int # type: ignore
194208 self ._alias = arg2
195209 self .header = \
196210 (control << 12 ) | (self ._alias & 0xFFF ) | 0x10_00_00_00
@@ -203,9 +217,10 @@ def __init__(self, *args, afterSendState=None, reservation=None,
203217
204218 if args_error :
205219 raise TypeError (
206- args_error .rstrip ("." ) + ". Valid constructors:"
207- + CanFrame .constructor_help () + ". Got: "
208- + openlcb .list_type_names (args ))
220+ "{}. Valid constructors: {}. Got: {}" .format (
221+ args_error .rstrip ("." ),
222+ CanFrame .constructor_help (),
223+ list_type_names (args )))
209224 if self ._alias is not None :
210225 if self ._alias & 0xFFF != self ._alias :
211226 raise ValueError (
0 commit comments