8
8
# pyright: reportGeneralTypeIssues=false
9
9
10
10
import calendar
11
+ import decimal
11
12
import functools
12
13
import sys
13
14
import logging
@@ -144,6 +145,8 @@ def default(self, o): # pylint: disable=too-many-return-statements
144
145
except TypeError :
145
146
if isinstance (o , _Null ):
146
147
return None
148
+ if isinstance (o , decimal .Decimal ):
149
+ return float (o )
147
150
if isinstance (o , (bytes , bytearray )):
148
151
return _serialize_bytes (o , self .format )
149
152
try :
@@ -275,6 +278,12 @@ def _deserialize_duration(attr):
275
278
return isodate .parse_duration (attr )
276
279
277
280
281
+ def _deserialize_decimal (attr ):
282
+ if isinstance (attr , decimal .Decimal ):
283
+ return attr
284
+ return decimal .Decimal (str (attr ))
285
+
286
+
278
287
_DESERIALIZE_MAPPING = {
279
288
datetime : _deserialize_datetime ,
280
289
date : _deserialize_date ,
@@ -283,6 +292,7 @@ def _deserialize_duration(attr):
283
292
bytearray : _deserialize_bytes ,
284
293
timedelta : _deserialize_duration ,
285
294
typing .Any : lambda x : x ,
295
+ decimal .Decimal : _deserialize_decimal ,
286
296
}
287
297
288
298
_DESERIALIZE_MAPPING_WITHFORMAT = {
@@ -426,6 +436,8 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
426
436
return tuple (_serialize (x , format ) for x in o )
427
437
if isinstance (o , (bytes , bytearray )):
428
438
return _serialize_bytes (o , format )
439
+ if isinstance (o , decimal .Decimal ):
440
+ return float (o )
429
441
try :
430
442
# First try datetime.datetime
431
443
return _serialize_datetime (o , format )
@@ -642,24 +654,18 @@ def _deserialize_with_union(deserializers, obj):
642
654
643
655
try :
644
656
if annotation ._name == "Dict" :
645
- key_deserializer = _get_deserialize_callable_from_annotation (annotation .__args__ [0 ], module , rf )
646
657
value_deserializer = _get_deserialize_callable_from_annotation (annotation .__args__ [1 ], module , rf )
647
658
648
659
def _deserialize_dict (
649
- key_deserializer : typing .Optional [typing .Callable ],
650
660
value_deserializer : typing .Optional [typing .Callable ],
651
661
obj : typing .Dict [typing .Any , typing .Any ],
652
662
):
653
663
if obj is None :
654
664
return obj
655
- return {
656
- _deserialize (key_deserializer , k , module ): _deserialize (value_deserializer , v , module )
657
- for k , v in obj .items ()
658
- }
665
+ return {k : _deserialize (value_deserializer , v , module ) for k , v in obj .items ()}
659
666
660
667
return functools .partial (
661
668
_deserialize_dict ,
662
- key_deserializer ,
663
669
value_deserializer ,
664
670
)
665
671
except (AttributeError , IndexError ):
@@ -698,27 +704,29 @@ def _deserialize_sequence(
698
704
pass
699
705
700
706
def _deserialize_default (
701
- annotation ,
702
- deserializer_from_mapping ,
707
+ deserializer ,
703
708
obj ,
704
709
):
705
710
if obj is None :
706
711
return obj
707
712
try :
708
- return _deserialize_with_callable (annotation , obj )
713
+ return _deserialize_with_callable (deserializer , obj )
709
714
except Exception :
710
715
pass
711
- return _deserialize_with_callable (deserializer_from_mapping , obj )
716
+ return obj
717
+
718
+ if get_deserializer (annotation , rf ):
719
+ return functools .partial (_deserialize_default , get_deserializer (annotation , rf ))
712
720
713
- return functools .partial (_deserialize_default , annotation , get_deserializer ( annotation , rf ) )
721
+ return functools .partial (_deserialize_default , annotation )
714
722
715
723
716
724
def _deserialize_with_callable (
717
725
deserializer : typing .Optional [typing .Callable [[typing .Any ], typing .Any ]],
718
726
value : typing .Any ,
719
727
):
720
728
try :
721
- if value is None :
729
+ if value is None or isinstance ( value , _Null ) :
722
730
return None
723
731
if deserializer is None :
724
732
return value
@@ -746,7 +754,8 @@ def _deserialize(
746
754
value = value .http_response .json ()
747
755
if rf is None and format :
748
756
rf = _RestField (format = format )
749
- deserializer = _get_deserialize_callable_from_annotation (deserializer , module , rf )
757
+ if not isinstance (deserializer , functools .partial ):
758
+ deserializer = _get_deserialize_callable_from_annotation (deserializer , module , rf )
750
759
return _deserialize_with_callable (deserializer , value )
751
760
752
761
0 commit comments