10
10
from typing import Any , List , Optional , Tuple , Union
11
11
12
12
from aiohttp import ClientSession , ClientTimeout # type: ignore
13
- from broadcast_wsrpc .client import Client , JsonDict , JsonType
13
+ from broadcast_wsrpc .client import Client
14
+ from broadcast_wsrpc .json import JsonObject , JsonType
14
15
15
16
16
17
class ComSrvError (Exception ):
@@ -20,9 +21,11 @@ class ComSrvError(Exception):
20
21
"""
21
22
22
23
@classmethod
23
- def parse (cls , data : JsonDict ) -> "ComSrvError" :
24
+ def parse (cls , data : JsonObject ) -> "ComSrvError" :
24
25
if "Protocol" in data :
25
- return ProtocolError .parse (data ["Protocol" ])
26
+ protocol = data ["Protocol" ]
27
+ assert isinstance (protocol , dict )
28
+ return ProtocolError .parse (protocol )
26
29
if "Transport" in data :
27
30
return TransportError (data ["Transport" ])
28
31
if "Argument" in data :
@@ -32,9 +35,11 @@ def parse(cls, data: JsonDict) -> "ComSrvError":
32
35
return ComSrvError (data )
33
36
34
37
@classmethod
35
- def check_raise (cls , result : JsonDict ) -> None :
38
+ def check_raise (cls , result : JsonObject ) -> None :
36
39
if "Error" in result :
37
- raise ComSrvError .parse (result ["Error" ])
40
+ err = result ["Error" ]
41
+ assert isinstance (err , dict )
42
+ raise ComSrvError .parse (err )
38
43
39
44
40
45
class ArgumentError (ComSrvError ):
@@ -60,12 +65,15 @@ class ProtocolError(ComSrvError):
60
65
"""
61
66
62
67
@classmethod
63
- def parse (cls , data : JsonDict ) -> "ProtocolError" :
68
+ def parse (cls , data : JsonObject ) -> "ProtocolError" :
64
69
if "Timeout" in data :
65
70
return ProtocolTimeoutError ()
66
71
if "Other" in data :
72
+ other = data ["Other" ]
73
+ assert isinstance (other , dict )
67
74
return ProtocolError (
68
- data ["Other" ]["description" ], data ["Other" ]["backtrace" ]
75
+ other ["description" ],
76
+ other ["backtrace" ],
69
77
)
70
78
return ProtocolError (data )
71
79
@@ -156,7 +164,7 @@ class Rpc(object):
156
164
Base class for RPC service implementations.
157
165
"""
158
166
159
- async def get (self , request : JsonType , timeout : float ) -> JsonDict :
167
+ async def get (self , request : JsonType , timeout : float ) -> JsonObject :
160
168
"""
161
169
Send a request and wait for the response, but for at most `timeout`.
162
170
"""
@@ -184,7 +192,7 @@ def __init__(self, url: str | None = None) -> None:
184
192
url = get_default_http_url ()
185
193
self ._url = url
186
194
187
- async def get (self , request : JsonType , timeout : float ) -> JsonDict :
195
+ async def get (self , request : JsonType , timeout : float ) -> JsonObject :
188
196
data = json .dumps (request ).encode ()
189
197
to = ClientTimeout (total = None , sock_connect = timeout , sock_read = timeout )
190
198
async with ClientSession (timeout = to ) as session :
@@ -216,9 +224,9 @@ def __init__(self, url: str | None = None, **kw: Any) -> None:
216
224
self ._kw = kw
217
225
self ._client = Client ()
218
226
219
- async def get (self , data : JsonType , timeout : float ) -> JsonDict :
227
+ async def get (self , request : JsonType , timeout : float ) -> JsonObject :
220
228
await self .connect (self ._url )
221
- ret = await self ._client .request (data , timeout )
229
+ ret = await self ._client .request (request , timeout )
222
230
if not isinstance (ret , dict ):
223
231
raise ComSrvError (f"Got wrong JSON type. Expected dict, got { type (ret )} " )
224
232
return ret
@@ -248,7 +256,7 @@ class Address(object):
248
256
def to_json (self ) -> JsonType :
249
257
raise NotImplementedError
250
258
251
- def to_json_enum (self ) -> JsonDict :
259
+ def to_json_enum (self ) -> JsonObject :
252
260
return {self .enum_name : self .to_json ()}
253
261
254
262
@property
@@ -271,7 +279,7 @@ def to_json(self) -> JsonType:
271
279
raise NotImplementedError
272
280
273
281
274
- def duration_to_json (time_in_seconds : float ) -> JsonDict :
282
+ def duration_to_json (time_in_seconds : float ) -> JsonObject :
275
283
"""
276
284
Serialize a duration in seconds to a RPC `Duration` object.
277
285
"""
@@ -294,7 +302,7 @@ def __init__(self, address: Address, rpc: Optional[Rpc] = None):
294
302
rpc = Rpc .make_default ()
295
303
self ._lock_time = 1.0
296
304
self ._address = address
297
- self ._lock = None
305
+ self ._lock : str | None = None
298
306
self ._rpc = rpc
299
307
self ._timeout = BasePipe .DEFAULT_TIMEOUT
300
308
@@ -357,14 +365,18 @@ async def lock(self, timeout: Union[float, None] = None) -> "BasePipe":
357
365
},
358
366
timeout = lock_time ,
359
367
)
360
- self ._lock = reply ["Locked" ]["lock_id" ]
368
+ lock = reply ["Locked" ]
369
+ assert isinstance (lock , dict ) and "lock_id" in lock
370
+ lock_id = lock ["lock_id" ]
371
+ assert isinstance (lock_id , str )
372
+ self ._lock = lock_id
361
373
return self
362
374
363
375
@property
364
376
def lock_id (self ) -> str | None :
365
377
return self ._lock
366
378
367
- async def get (self , data : JsonType , timeout : float | None = None ) -> JsonDict :
379
+ async def get (self , data : JsonType , timeout : float | None = None ) -> JsonObject :
368
380
"""
369
381
Send a request and return the corresponding response but wait for at most `timeout`
370
382
seconds.
@@ -479,7 +491,7 @@ def timeout(self, value: float) -> None:
479
491
def rpc (self ) -> Rpc :
480
492
return self ._rpc
481
493
482
- async def get (self , data : JsonType , timeout : float | None = None ) -> JsonDict :
494
+ async def get (self , data : JsonType , timeout : float | None = None ) -> JsonObject :
483
495
if timeout is None :
484
496
timeout = self ._timeout
485
497
return await self ._rpc .get (data , timeout )
@@ -491,11 +503,15 @@ async def drop(self, addr: Address, lock: str | None = None) -> None:
491
503
async def get_version (self ) -> Tuple [int , int , int ]:
492
504
result = await self .get ({"Version" : None })
493
505
ComSrvError .check_raise (result )
494
- return (
495
- result ["Version" ]["major" ],
496
- result ["Version" ]["minor" ],
497
- result ["Version" ]["build" ],
506
+ version = result ["Version" ]
507
+ assert isinstance (version , dict )
508
+ major = version ["major" ]
509
+ minor = version ["minor" ]
510
+ build = version ["build" ]
511
+ assert (
512
+ isinstance (major , int ) and isinstance (minor , int ) and isinstance (build , int )
498
513
)
514
+ return (major , minor , build )
499
515
500
516
async def drop_all (self ) -> None :
501
517
result = await self .get ({"DropAll" : None })
@@ -505,10 +521,12 @@ async def shutdown(self) -> None:
505
521
result = await self .get ({"Shutdown" : None })
506
522
ComSrvError .check_raise (result )
507
523
508
- async def list_connected_instruments (self ) -> List [JsonDict ]:
524
+ async def list_connected_instruments (self ) -> List [JsonObject ]:
509
525
result = await self .get ({"ListConnectedInstruments" : None })
510
526
ComSrvError .check_raise (result )
511
- return result ["Instruments" ]
527
+ instruments = result ["Instruments" ]
528
+ assert isinstance (instruments , list )
529
+ return instruments # type: ignore
512
530
513
531
async def list_hid_devices (self ) -> List [HidDeviceInfo ]:
514
532
from .hid import enumerate_hid_devices
@@ -518,15 +536,19 @@ async def list_hid_devices(self) -> List[HidDeviceInfo]:
518
536
async def list_serial_ports (self ) -> List [str ]:
519
537
result = await self .get ({"ListSerialPorts" : None })
520
538
ComSrvError .check_raise (result )
521
- return result ["SerialPorts" ]
539
+ serial_ports = result ["SerialPorts" ]
540
+ assert isinstance (serial_ports , list )
541
+ return serial_ports # type: ignore
522
542
523
543
async def list_ftdis (self ) -> List [FtdiDeviceInfo ]:
524
544
result = await self .get ({"ListFtdiDevices" : None })
525
545
ComSrvError .check_raise (result )
526
546
ret = []
527
- for x in result ["FtdiDevices" ]:
528
- ret .append (FtdiDeviceInfo (** x ))
529
- return ret
547
+ ftdi_devices = result ["FtdiDevices" ]
548
+ assert isinstance (ftdi_devices , list )
549
+ for x in ftdi_devices :
550
+ ret .append (FtdiDeviceInfo (** x )) # type: ignore
551
+ return ret # type: ignore
530
552
531
553
async def list_can_devices (self ) -> List [CanDevice ]:
532
554
result = await self .get (
@@ -536,13 +558,15 @@ async def list_can_devices(self) -> List[CanDevice]:
536
558
)
537
559
ComSrvError .check_raise (result )
538
560
ret = []
539
- for x in result ["CanDevices" ]:
540
- if x ["driver_type" ] == "SocketCAN" :
561
+ can_devices = result ["CanDevices" ]
562
+ assert isinstance (can_devices , list )
563
+ for x in can_devices :
564
+ if x ["driver_type" ] == "SocketCAN" : # type: ignore
541
565
driver_type = CanDriverType .SOCKETCAN
542
- elif x ["driver_type" ] == "PCAN" :
566
+ elif x ["driver_type" ] == "PCAN" : # type: ignore
543
567
driver_type = CanDriverType .PCAN
544
- ret .append (CanDevice (x ["interface_name" ], driver_type = driver_type ))
545
- return ret
568
+ ret .append (CanDevice (x ["interface_name" ], driver_type = driver_type )) # type: ignore
569
+ return ret # type: ignore
546
570
547
571
548
572
from .bytestream import ( # noqa: E402
0 commit comments