7
7
import typing
8
8
import warnings
9
9
from types import ModuleType
10
- from typing import Type , Optional , Callable , Generator , Any , Dict , Match , List , Union , Generic
10
+ from typing import Type , Optional , Callable , Generator , Dict , Match , List , Union , Generic
11
11
12
12
from bunq .sdk .exception .bunq_exception import BunqException
13
13
from bunq .sdk .util .type_alias import T , JsonValue
@@ -74,14 +74,14 @@ def register_custom_adapter(cls,
74
74
cls ._custom_deserializers [class_name ] = adapter
75
75
76
76
@classmethod
77
- def _get_serializer (cls , cls_for : Type [Any ]) -> type :
77
+ def _get_serializer (cls , cls_for : Type [T ]) -> type :
78
78
if cls_for .__name__ in cls ._custom_serializers :
79
79
return cls ._custom_serializers [cls_for .__name__ ]
80
80
81
81
return JsonAdapter
82
82
83
83
@classmethod
84
- def _get_deserializer (cls , cls_for : Type [Any ]) -> Type [JsonAdapter ]:
84
+ def _get_deserializer (cls , cls_for : Type [T ]) -> Type [JsonAdapter ]:
85
85
if cls_for .__name__ in cls ._custom_deserializers :
86
86
return cls ._custom_deserializers [cls_for .__name__ ]
87
87
@@ -121,7 +121,7 @@ def _deserialize_default(cls,
121
121
@classmethod
122
122
def _is_deserialized (cls ,
123
123
cls_target : Type [T ],
124
- obj : Any ) -> bool :
124
+ obj : T ) -> bool :
125
125
if cls_target is None :
126
126
return True
127
127
@@ -151,7 +151,7 @@ def _deserialize_dict(cls,
151
151
152
152
@classmethod
153
153
def _deserialize_dict_attributes (cls ,
154
- cls_context : Type [Any ],
154
+ cls_context : Type [T ],
155
155
dict_ : Dict ) -> Dict :
156
156
dict_deserialized = {}
157
157
@@ -243,7 +243,7 @@ def _str_to_type(cls,
243
243
@classmethod
244
244
def _str_to_type_from_member_module (cls ,
245
245
module_ : ModuleType ,
246
- string : str ) -> Type [Any ]:
246
+ string : str ) -> Type [T ]:
247
247
"""
248
248
249
249
:raise: BunqException when could not find the class for the string.
@@ -307,7 +307,7 @@ def can_serialize(cls) -> bool:
307
307
return True
308
308
309
309
@classmethod
310
- def serialize (cls , obj : Any ) -> JsonValue :
310
+ def serialize (cls , obj : T ) -> JsonValue :
311
311
cls ._initialize ()
312
312
serializer = cls ._get_serializer (type (obj ))
313
313
@@ -317,7 +317,7 @@ def serialize(cls, obj: Any) -> JsonValue:
317
317
return serializer .serialize (obj )
318
318
319
319
@classmethod
320
- def _serialize_default (cls , obj : Any ) -> JsonValue :
320
+ def _serialize_default (cls , obj : T ) -> JsonValue :
321
321
if obj is None or cls ._is_primitive (obj ):
322
322
return obj
323
323
elif cls ._is_bytes (obj ):
@@ -330,19 +330,19 @@ def _serialize_default(cls, obj: Any) -> JsonValue:
330
330
return cls ._serialize_dict (dict_ )
331
331
332
332
@classmethod
333
- def _is_primitive (cls , obj : Any ) -> bool :
333
+ def _is_primitive (cls , obj : T ) -> bool :
334
334
return cls ._is_type_primitive (type (obj ))
335
335
336
336
@classmethod
337
- def _is_type_primitive (cls , type_ : Type [Any ]) -> bool :
337
+ def _is_type_primitive (cls , type_ : Type [T ]) -> bool :
338
338
return type_ in {int , str , bool , float }
339
339
340
340
@classmethod
341
- def _is_bytes (cls , obj : Any ) -> bool :
341
+ def _is_bytes (cls , obj : T ) -> bool :
342
342
return cls ._is_bytes_type (type (obj ))
343
343
344
344
@classmethod
345
- def _is_bytes_type (cls , type_ : Type [Any ]) -> bool :
345
+ def _is_bytes_type (cls , type_ : Type [T ]) -> bool :
346
346
return type_ .__name__ in cls ._TYPE_NAMES_BYTES
347
347
348
348
@classmethod
@@ -356,7 +356,7 @@ def _serialize_list(cls, list_: List) -> List:
356
356
return list_serialized
357
357
358
358
@classmethod
359
- def _get_obj_raw (cls , obj : Any ) -> Dict :
359
+ def _get_obj_raw (cls , obj : T ) -> Dict :
360
360
return obj if type (obj ) == dict else obj .__dict__
361
361
362
362
@classmethod
@@ -375,32 +375,22 @@ def _serialize_dict(cls, dict_: Dict) -> Dict:
375
375
376
376
377
377
class ValueTypes :
378
- """
379
- :type _main: type|None
380
- :type _sub: type|None
381
- """
382
-
383
378
def __init__ (self ,
384
- main : Type [Any ] = None ,
385
- sub : Type [Any ] = None ) -> None :
379
+ main : Type [T ] = None ,
380
+ sub : Type [T ] = None ) -> None :
386
381
self ._main = main
387
382
self ._sub = sub
388
383
389
384
@property
390
- def main (self ) -> Type [Any ]:
385
+ def main (self ) -> Type [T ]:
391
386
return self ._main
392
387
393
388
@property
394
- def sub (self ) -> Type [Any ]:
389
+ def sub (self ) -> Type [T ]:
395
390
return self ._sub
396
391
397
392
398
393
class ValueSpecs :
399
- """
400
- :type _name: str|None
401
- :type _types: ValueTypes|None
402
- """
403
-
404
394
def __init__ (self ,
405
395
name : str = None ,
406
396
types : ValueTypes = None ) -> None :
@@ -444,11 +434,11 @@ def deserialize(cls: Type[T], obj_raw: JsonValue) -> T:
444
434
return JsonAdapter .deserialize (cls , obj_raw )
445
435
446
436
447
- def class_to_json (obj : Any ) -> JsonValue :
437
+ def class_to_json (obj : T ) -> JsonValue :
448
438
obj_raw = serialize (obj )
449
439
450
440
return json .dumps (obj_raw , indent = _JSON_INDENT , sort_keys = True )
451
441
452
442
453
- def serialize (obj_cls : Any ) -> JsonValue :
443
+ def serialize (obj_cls : T ) -> JsonValue :
454
444
return JsonAdapter .serialize (obj_cls )
0 commit comments