Skip to content

Commit 5290157

Browse files
authored
V0.3 explicit typing module usage (#146)
* do, except core , client & param * core * core.zmq * client * from __future__ import Annotations for delayed evalutaion for specific files * change type to Type
1 parent 868e1da commit 5290157

31 files changed

+456
-472
lines changed

hololinked/client/abstractions.py

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import builtins
2828
import logging
2929
import threading
30-
import typing
3130

3231
from dataclasses import dataclass
32+
from typing import Any, Callable
3333

3434
from ..constants import Operations
3535
from ..td import ActionAffordance, EventAffordance, PropertyAffordance
@@ -44,16 +44,15 @@ class ConsumedThingAction:
4444
def __init__(
4545
self,
4646
resource: ActionAffordance,
47-
owner_inst: typing.Any,
47+
owner_inst: Any,
4848
logger: logging.Logger,
49-
# schema_validator: typing.Type[BaseSchemaValidator] | None = None
5049
) -> None:
5150
"""
5251
Parameters
5352
----------
5453
resource: ActionAffordance
5554
dataclass TD fragment representing the action (must have forms).
56-
owner_inst: typing.Optional[typing.Any]
55+
owner_inst: Any
5756
instance of the owning consumed Thing or `ObjectProxy`
5857
logger: logging.Logger
5958
logger instance
@@ -63,7 +62,7 @@ def __init__(
6362
self.logger = logger
6463
self.schema_validator = None # schema_validator
6564

66-
def get_last_return_value(self, raise_exception: bool = False) -> typing.Any:
65+
def get_last_return_value(self, raise_exception: bool = False) -> Any:
6766
"""retrieve return value of the last call to the action"""
6867
raise NotImplementedError("implement get_last_return_value per protocol")
6968

@@ -73,38 +72,38 @@ def get_last_return_value(self, raise_exception: bool = False) -> typing.Any:
7372
)
7473
"""cached return value of the last call to the method"""
7574

76-
def __call__(self, *args, **kwargs) -> typing.Any:
75+
def __call__(self, *args, **kwargs) -> Any:
7776
"""
7877
Invoke action/method on server
7978
8079
Parameters
8180
----------
82-
*args: typing.Any
81+
*args: Any
8382
arguments to the action
84-
**kwargs: typing.Any
83+
**kwargs: Any
8584
keyword arguments to the action
8685
8786
Returns
8887
-------
89-
typing.Any
88+
Any
9089
reply of the action call
9190
"""
9291
raise NotImplementedError("implement action __call__ per protocol")
9392

94-
async def async_call(self, *args, **kwargs) -> typing.Any:
93+
async def async_call(self, *args, **kwargs) -> Any:
9594
"""
9695
async invoke action on server - asynchronous at the network level, may not necessarily be at the server level.
9796
9897
Parameters
9998
----------
100-
*args: typing.Any
99+
*args: Any
101100
arguments to the action
102-
**kwargs: typing.Any
101+
**kwargs: Any
103102
keyword arguments to the action
104103
105104
Returns
106105
-------
107-
typing.Any
106+
Any
108107
reply of the action call
109108
"""
110109
raise NotImplementedError("implement action async_call per protocol")
@@ -116,9 +115,9 @@ def oneway(self, *args, **kwargs) -> None:
116115
117116
Parameters
118117
----------
119-
*args: typing.Any
118+
*args: Any
120119
arguments to the action
121-
**kwargs: typing.Any
120+
**kwargs: Any
122121
keyword arguments to the action
123122
"""
124123
raise NotImplementedError("implement action oneway call per protocol")
@@ -130,9 +129,9 @@ def noblock(self, *args, **kwargs) -> str:
130129
131130
Parameters
132131
----------
133-
*args: typing.Any
132+
*args: Any
134133
arguments to the action
135-
**kwargs: typing.Any
134+
**kwargs: Any
136135
keyword arguments to the action
137136
138137
Returns
@@ -142,7 +141,7 @@ def noblock(self, *args, **kwargs) -> str:
142141
"""
143142
raise NotImplementedError("implement action noblock call per protocol")
144143

145-
def read_reply(self, message_id: str, timeout: float | int | None = None) -> typing.Any:
144+
def read_reply(self, message_id: str, timeout: float | int | None = None) -> Any:
146145
"""
147146
Read the reply of the action call which was scheduled with `noblock`.
148147
@@ -155,7 +154,7 @@ def read_reply(self, message_id: str, timeout: float | int | None = None) -> typ
155154
156155
Returns
157156
-------
158-
typing.Any
157+
Any
159158
reply of the action call
160159
"""
161160
raise NotImplementedError("implement action read_reply per protocol")
@@ -173,15 +172,13 @@ class ConsumedThingProperty:
173172
# property get set abstraction
174173
# Dont add doc otherwise __doc__ in slots will conflict with class variable
175174

176-
def __init__(
177-
self, resource: PropertyAffordance, owner_inst: typing.Optional[typing.Any], logger: logging.Logger
178-
) -> None:
175+
def __init__(self, resource: PropertyAffordance, owner_inst: Any, logger: logging.Logger) -> None:
179176
"""
180177
Parameters
181178
----------
182179
resource: PropertyAffordance
183180
dataclass object TD fragment representing the property (must have forms).
184-
owner_inst: typing.Optional[typing.Any]
181+
owner_inst: Any
185182
instance of the owning consumed Thing or `ObjectProxy`
186183
logger: logging.Logger
187184
logger instance
@@ -191,50 +188,50 @@ def __init__(
191188
self.logger = logger
192189

193190
@property # i.e. cannot have setter
194-
def last_read_value(self) -> typing.Any:
191+
def last_read_value(self) -> Any:
195192
"""cache of last read value"""
196193
raise NotImplementedError("implement last_read_value per protocol")
197194

198-
def set(self, value: typing.Any) -> None:
195+
def set(self, value: Any) -> None:
199196
"""
200197
Set or write property value.
201198
202199
Parameters
203200
----------
204-
value: typing.Any
201+
value: Any
205202
value to set
206203
"""
207204
raise NotImplementedError("implement property set per protocol")
208205

209-
def get(self) -> typing.Any:
206+
def get(self) -> Any:
210207
"""
211208
Get or read property value.
212209
213210
Returns
214211
-------
215-
typing.Any
212+
Any
216213
property value
217214
"""
218215
raise NotImplementedError("implement property get per protocol")
219216

220-
async def async_set(self, value: typing.Any) -> None:
217+
async def async_set(self, value: Any) -> None:
221218
"""
222219
Async set or write property value - asynchronous at the network level, may not necessarily be at the server level.
223220
224221
Parameters
225222
----------
226-
value: typing.Any
223+
value: Any
227224
value to set
228225
"""
229226
raise NotImplementedError("implement async property set per protocol")
230227

231-
async def async_get(self) -> typing.Any:
228+
async def async_get(self) -> Any:
232229
"""
233230
Async get or read property value - asynchronous at the network level, may not necessarily be at the server level.
234231
235232
Returns
236233
-------
237-
typing.Any
234+
Any
238235
property value
239236
"""
240237
raise NotImplementedError("implement async property get per protocol")
@@ -251,14 +248,14 @@ def noblock_get(self) -> str:
251248
"""
252249
raise NotImplementedError("implement property noblock get per protocol")
253250

254-
def noblock_set(self, value: typing.Any) -> str:
251+
def noblock_set(self, value: Any) -> str:
255252
"""
256253
Set or write property value without blocking, i.e. make a request and collect it later and the method returns immediately.
257254
Server must return a message ID to identify the request.
258255
259256
Parameters
260257
----------
261-
value: typing.Any
258+
value: Any
262259
value to set
263260
264261
Returns
@@ -268,25 +265,25 @@ def noblock_set(self, value: typing.Any) -> str:
268265
"""
269266
raise NotImplementedError("implement property noblock set per protocol")
270267

271-
def oneway_set(self, value: typing.Any) -> None:
268+
def oneway_set(self, value: Any) -> None:
272269
"""
273270
Set property value without waiting for acknowledgement. The server also does not send any reply.
274271
There is no guarantee that the property value was set.
275272
276273
Parameters
277274
----------
278-
value: typing.Any
275+
value: Any
279276
value to set
280277
"""
281278
raise NotImplementedError("implement property oneway set per protocol")
282279

283-
def observe(self, *callbacks: typing.Callable) -> None:
280+
def observe(self, *callbacks: Callable) -> None:
284281
"""
285282
Observe property value changes
286283
287284
Parameters
288285
----------
289-
*callbacks: typing.Callable
286+
*callbacks: Callable
290287
callback to call when property value changes
291288
"""
292289
# looks like this will be unused. observe property is done via ConsumedThingEvent
@@ -297,7 +294,7 @@ def unobserve(self) -> None:
297294
# looks like this will be unused, observe property is done via ConsumedThingEvent
298295
raise NotImplementedError("implement property unobserve per protocol")
299296

300-
def read_reply(self, message_id: str, timeout: float | int | None = None) -> typing.Any:
297+
def read_reply(self, message_id: str, timeout: float | int | None = None) -> Any:
301298
"""
302299
Read the reply of the property get or set which was scheduled with `noblock`.
303300
@@ -310,7 +307,7 @@ def read_reply(self, message_id: str, timeout: float | int | None = None) -> typ
310307
311308
Returns
312309
-------
313-
typing.Any
310+
Any
314311
reply of the property get or set
315312
"""
316313
raise NotImplementedError("implement property read_reply per protocol")
@@ -324,7 +321,7 @@ def __init__(
324321
self,
325322
resource: EventAffordance,
326323
logger: logging.Logger,
327-
owner_inst: typing.Any,
324+
owner_inst: Any,
328325
) -> None:
329326
"""
330327
Parameters
@@ -333,7 +330,7 @@ def __init__(
333330
dataclass object representing the event
334331
logger: logging.Logger
335332
logger instance
336-
owner_inst: typing.Any
333+
owner_inst: Any
337334
the parent object that owns this event
338335
"""
339336
self.resource = resource
@@ -345,7 +342,7 @@ def __init__(
345342

346343
def subscribe(
347344
self,
348-
callbacks: list[typing.Callable] | typing.Callable,
345+
callbacks: list[Callable] | Callable,
349346
asynch: bool = False,
350347
concurrent: bool = False,
351348
deserialize: bool = True,
@@ -356,7 +353,7 @@ def subscribe(
356353
357354
Parameters
358355
----------
359-
callbacks: typing.List[typing.Callable] | typing.Callable
356+
callbacks: list[Callable] | Callable
360357
callback or list of callbacks to add
361358
asynch: bool
362359
whether to start an async(-io task) event listener instead of a threaded listener
@@ -387,27 +384,27 @@ def unsubscribe(self):
387384
# self._sync_callbacks.clear()
388385
# self._async_callbacks.clear()
389386

390-
def listen(self, form: Form, callbacks: list[typing.Callable], concurrent: bool = True, deserialize: bool = True):
387+
def listen(self, form: Form, callbacks: list[Callable], concurrent: bool = True, deserialize: bool = True):
391388
"""
392389
listen to events and call the callbacks
393390
"""
394391
raise NotImplementedError("implement listen per protocol")
395392

396393
async def async_listen(
397-
self, form: Form, callbacks: list[typing.Callable], concurrent: bool = True, deserialize: bool = True
394+
self, form: Form, callbacks: list[Callable], concurrent: bool = True, deserialize: bool = True
398395
):
399396
"""
400397
listen to events and call the callbacks
401398
"""
402399
raise NotImplementedError("implement async_listen per protocol")
403400

404-
def schedule_callbacks(self, callbacks, event_data: typing.Any, concurrent: bool = False) -> None:
401+
def schedule_callbacks(self, callbacks, event_data: Any, concurrent: bool = False) -> None:
405402
"""
406403
schedule the callbacks to be called with the event data
407404
408405
Parameters
409406
----------
410-
event_data: typing.Any
407+
event_data: Any
411408
event data to pass to the callbacks
412409
concurrent: bool
413410
whether to run each callback in a separate thread
@@ -422,13 +419,13 @@ def schedule_callbacks(self, callbacks, event_data: typing.Any, concurrent: bool
422419
self.logger.error(f"Error occurred in callback {cb}: {ex}")
423420
self.logger.exception(ex)
424421

425-
async def async_schedule_callbacks(self, callbacks, event_data: typing.Any, concurrent: bool = False) -> None:
422+
async def async_schedule_callbacks(self, callbacks, event_data: Any, concurrent: bool = False) -> None:
426423
"""
427424
schedule the callbacks to be called with the event data
428425
429426
Parameters
430427
----------
431-
event_data: typing.Any
428+
event_data: Any
432429
event data to pass to the callbacks
433430
concurrent: bool
434431
whether to run each callback in a separate thread
@@ -449,15 +446,13 @@ async def async_schedule_callbacks(self, callbacks, event_data: typing.Any, conc
449446
self.logger.error(f"Error occurred in callback {cb}: {ex}")
450447
self.logger.exception(ex)
451448

452-
def add_callbacks(
453-
self, callbacks: typing.Union[typing.List[typing.Callable], typing.Callable], asynch: bool = False
454-
) -> None:
449+
def add_callbacks(self, callbacks: list[Callable] | Callable, asynch: bool = False) -> None:
455450
"""
456451
add callbacks to the event
457452
458453
Parameters
459454
----------
460-
*callbacks: typing.List[typing.Callable] | typing.Callable
455+
*callbacks: list[Callable] | Callable
461456
callback or list of callbacks to add
462457
"""
463458
raise NotImplementedError(
@@ -466,7 +461,7 @@ def add_callbacks(
466461
# for logic, see tag v0.3.2
467462

468463

469-
def raise_local_exception(error_message: typing.Dict[str, typing.Any]) -> None:
464+
def raise_local_exception(error_message: dict[str, Any]) -> None:
470465
"""
471466
raises an exception on client side using an exception from server, using a mapping based on exception type
472467
(currently only python built-in exceptions supported). If the exception type is not found, a generic `Exception` is raised.
@@ -525,11 +520,11 @@ def __init__(self):
525520
def clear(self):
526521
"""reset to default/empty values"""
527522
self.event = "message" # type: str
528-
self.data = "" # type: typing.Any
529-
self.id = None # type: typing.Optional[str]
530-
self.retry = None # type: typing.Optional[int]
523+
self.data = "" # type: Any
524+
self.id = None # type: str | None
525+
self.retry = None # type: int | None
531526

532-
def flush(self) -> typing.Optional[dict]:
527+
def flush(self) -> dict[str, Any] | None:
533528
"""obtain the event payload as dictionary and reset to default values"""
534529
if not self.data and self.id is None:
535530
return None

0 commit comments

Comments
 (0)