1
- # pylint: disable=too-many-lines
1
+ # pylint: disable=line-too-long,useless-suppression, too-many-lines
2
2
# coding=utf-8
3
3
# --------------------------------------------------------------------------
4
4
# Copyright (c) Microsoft Corporation. All rights reserved.
29
29
from azure .core import CaseInsensitiveEnumMeta
30
30
from azure .core .pipeline import PipelineResponse
31
31
from azure .core .serialization import _Null
32
+ from azure .core .rest import HttpResponse
32
33
33
34
_LOGGER = logging .getLogger (__name__ )
34
35
@@ -345,7 +346,7 @@ def _get_model(module_name: str, model_name: str):
345
346
346
347
347
348
class _MyMutableMapping (MutableMapping [str , typing .Any ]):
348
- def __init__ (self , data : typing . Dict [str , typing .Any ]) -> None :
349
+ def __init__ (self , data : dict [str , typing .Any ]) -> None :
349
350
self ._data = data
350
351
351
352
def __contains__ (self , key : typing .Any ) -> bool :
@@ -425,7 +426,7 @@ def pop(self, key: str, default: typing.Any = _UNSET) -> typing.Any:
425
426
return self ._data .pop (key )
426
427
return self ._data .pop (key , default )
427
428
428
- def popitem (self ) -> typing . Tuple [str , typing .Any ]:
429
+ def popitem (self ) -> tuple [str , typing .Any ]:
429
430
"""
430
431
Removes and returns some (key, value) pair
431
432
:returns: The (key, value) pair.
@@ -513,9 +514,7 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
513
514
return o
514
515
515
516
516
- def _get_rest_field (
517
- attr_to_rest_field : typing .Dict [str , "_RestField" ], rest_name : str
518
- ) -> typing .Optional ["_RestField" ]:
517
+ def _get_rest_field (attr_to_rest_field : dict [str , "_RestField" ], rest_name : str ) -> typing .Optional ["_RestField" ]:
519
518
try :
520
519
return next (rf for rf in attr_to_rest_field .values () if rf ._rest_name == rest_name )
521
520
except StopIteration :
@@ -538,7 +537,7 @@ class Model(_MyMutableMapping):
538
537
_is_model = True
539
538
# label whether current class's _attr_to_rest_field has been calculated
540
539
# could not see _attr_to_rest_field directly because subclass inherits it from parent class
541
- _calculated : typing . Set [str ] = set ()
540
+ _calculated : set [str ] = set ()
542
541
543
542
def __init__ (self , * args : typing .Any , ** kwargs : typing .Any ) -> None :
544
543
class_name = self .__class__ .__name__
@@ -623,7 +622,7 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self:
623
622
# we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping',
624
623
# 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object'
625
624
mros = cls .__mro__ [:- 9 ][::- 1 ] # ignore parents, and reverse the mro order
626
- attr_to_rest_field : typing . Dict [str , _RestField ] = { # map attribute name to rest_field property
625
+ attr_to_rest_field : dict [str , _RestField ] = { # map attribute name to rest_field property
627
626
k : v for mro_class in mros for k , v in mro_class .__dict__ .items () if k [0 ] != "_" and hasattr (v , "_type" )
628
627
}
629
628
annotations = {
@@ -638,7 +637,7 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self:
638
637
rf ._type = rf ._get_deserialize_callable_from_annotation (annotations .get (attr , None ))
639
638
if not rf ._rest_name_input :
640
639
rf ._rest_name_input = attr
641
- cls ._attr_to_rest_field : typing . Dict [str , _RestField ] = dict (attr_to_rest_field .items ())
640
+ cls ._attr_to_rest_field : dict [str , _RestField ] = dict (attr_to_rest_field .items ())
642
641
cls ._calculated .add (f"{ cls .__module__ } .{ cls .__qualname__ } " )
643
642
644
643
return super ().__new__ (cls )
@@ -680,7 +679,7 @@ def _deserialize(cls, data, exist_discriminators):
680
679
mapped_cls = cls .__mapping__ .get (discriminator_value , cls ) # pyright: ignore # pylint: disable=no-member
681
680
return mapped_cls ._deserialize (data , exist_discriminators )
682
681
683
- def as_dict (self , * , exclude_readonly : bool = False ) -> typing . Dict [str , typing .Any ]:
682
+ def as_dict (self , * , exclude_readonly : bool = False ) -> dict [str , typing .Any ]:
684
683
"""Return a dict that can be turned into json using json.dump.
685
684
686
685
:keyword bool exclude_readonly: Whether to remove the readonly properties.
@@ -740,7 +739,7 @@ def _deserialize_with_union(deserializers, obj):
740
739
def _deserialize_dict (
741
740
value_deserializer : typing .Optional [typing .Callable ],
742
741
module : typing .Optional [str ],
743
- obj : typing . Dict [typing .Any , typing .Any ],
742
+ obj : dict [typing .Any , typing .Any ],
744
743
):
745
744
if obj is None :
746
745
return obj
@@ -750,7 +749,7 @@ def _deserialize_dict(
750
749
751
750
752
751
def _deserialize_multiple_sequence (
753
- entry_deserializers : typing . List [typing .Optional [typing .Callable ]],
752
+ entry_deserializers : list [typing .Optional [typing .Callable ]],
754
753
module : typing .Optional [str ],
755
754
obj ,
756
755
):
@@ -771,14 +770,14 @@ def _deserialize_sequence(
771
770
return type (obj )(_deserialize (deserializer , entry , module ) for entry in obj )
772
771
773
772
774
- def _sorted_annotations (types : typing . List [typing .Any ]) -> typing . List [typing .Any ]:
773
+ def _sorted_annotations (types : list [typing .Any ]) -> list [typing .Any ]:
775
774
return sorted (
776
775
types ,
777
776
key = lambda x : hasattr (x , "__name__" ) and x .__name__ .lower () in ("str" , "float" , "int" , "bool" ),
778
777
)
779
778
780
779
781
- def _get_deserialize_callable_from_annotation ( # pylint: disable=too-many-return-statements, too-many-branches
780
+ def _get_deserialize_callable_from_annotation ( # pylint: disable=too-many-return-statements, too-many-statements, too-many- branches
782
781
annotation : typing .Any ,
783
782
module : typing .Optional [str ],
784
783
rf : typing .Optional ["_RestField" ] = None ,
@@ -843,7 +842,10 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur
843
842
return functools .partial (_deserialize_with_union , deserializers )
844
843
845
844
try :
846
- if annotation ._name == "Dict" : # pyright: ignore
845
+ annotation_name = (
846
+ annotation .__name__ if hasattr (annotation , "__name__" ) else annotation ._name # pyright: ignore
847
+ )
848
+ if annotation_name .lower () == "dict" :
847
849
value_deserializer = _get_deserialize_callable_from_annotation (
848
850
annotation .__args__ [1 ], module , rf # pyright: ignore
849
851
)
@@ -856,7 +858,10 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur
856
858
except (AttributeError , IndexError ):
857
859
pass
858
860
try :
859
- if annotation ._name in ["List" , "Set" , "Tuple" , "Sequence" ]: # pyright: ignore
861
+ annotation_name = (
862
+ annotation .__name__ if hasattr (annotation , "__name__" ) else annotation ._name # pyright: ignore
863
+ )
864
+ if annotation_name .lower () in ["list" , "set" , "tuple" , "sequence" ]:
860
865
if len (annotation .__args__ ) > 1 : # pyright: ignore
861
866
entry_deserializers = [
862
867
_get_deserialize_callable_from_annotation (dt , module , rf )
@@ -940,13 +945,13 @@ def _deserialize(
940
945
941
946
def _failsafe_deserialize (
942
947
deserializer : typing .Any ,
943
- value : typing . Any ,
948
+ response : HttpResponse ,
944
949
module : typing .Optional [str ] = None ,
945
950
rf : typing .Optional ["_RestField" ] = None ,
946
951
format : typing .Optional [str ] = None ,
947
952
) -> typing .Any :
948
953
try :
949
- return _deserialize (deserializer , value , module , rf , format )
954
+ return _deserialize (deserializer , response . json () , module , rf , format )
950
955
except DeserializationError :
951
956
_LOGGER .warning (
952
957
"Ran into a deserialization error. Ignoring since this is failsafe deserialization" , exc_info = True
@@ -956,10 +961,10 @@ def _failsafe_deserialize(
956
961
957
962
def _failsafe_deserialize_xml (
958
963
deserializer : typing .Any ,
959
- value : typing . Any ,
964
+ response : HttpResponse ,
960
965
) -> typing .Any :
961
966
try :
962
- return _deserialize_xml (deserializer , value )
967
+ return _deserialize_xml (deserializer , response . text () )
963
968
except DeserializationError :
964
969
_LOGGER .warning (
965
970
"Ran into a deserialization error. Ignoring since this is failsafe deserialization" , exc_info = True
@@ -974,11 +979,11 @@ def __init__(
974
979
name : typing .Optional [str ] = None ,
975
980
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
976
981
is_discriminator : bool = False ,
977
- visibility : typing .Optional [typing . List [str ]] = None ,
982
+ visibility : typing .Optional [list [str ]] = None ,
978
983
default : typing .Any = _UNSET ,
979
984
format : typing .Optional [str ] = None ,
980
985
is_multipart_file_input : bool = False ,
981
- xml : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
986
+ xml : typing .Optional [dict [str , typing .Any ]] = None ,
982
987
):
983
988
self ._type = type
984
989
self ._rest_name_input = name
@@ -1036,11 +1041,11 @@ def rest_field(
1036
1041
* ,
1037
1042
name : typing .Optional [str ] = None ,
1038
1043
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
1039
- visibility : typing .Optional [typing . List [str ]] = None ,
1044
+ visibility : typing .Optional [list [str ]] = None ,
1040
1045
default : typing .Any = _UNSET ,
1041
1046
format : typing .Optional [str ] = None ,
1042
1047
is_multipart_file_input : bool = False ,
1043
- xml : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
1048
+ xml : typing .Optional [dict [str , typing .Any ]] = None ,
1044
1049
) -> typing .Any :
1045
1050
return _RestField (
1046
1051
name = name ,
@@ -1057,8 +1062,8 @@ def rest_discriminator(
1057
1062
* ,
1058
1063
name : typing .Optional [str ] = None ,
1059
1064
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
1060
- visibility : typing .Optional [typing . List [str ]] = None ,
1061
- xml : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
1065
+ visibility : typing .Optional [list [str ]] = None ,
1066
+ xml : typing .Optional [dict [str , typing .Any ]] = None ,
1062
1067
) -> typing .Any :
1063
1068
return _RestField (name = name , type = type , is_discriminator = True , visibility = visibility , xml = xml )
1064
1069
@@ -1077,9 +1082,9 @@ def serialize_xml(model: Model, exclude_readonly: bool = False) -> str:
1077
1082
def _get_element (
1078
1083
o : typing .Any ,
1079
1084
exclude_readonly : bool = False ,
1080
- parent_meta : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
1085
+ parent_meta : typing .Optional [dict [str , typing .Any ]] = None ,
1081
1086
wrapped_element : typing .Optional [ET .Element ] = None ,
1082
- ) -> typing .Union [ET .Element , typing . List [ET .Element ]]:
1087
+ ) -> typing .Union [ET .Element , list [ET .Element ]]:
1083
1088
if _is_model (o ):
1084
1089
model_meta = getattr (o , "_xml" , {})
1085
1090
@@ -1168,7 +1173,7 @@ def _get_element(
1168
1173
def _get_wrapped_element (
1169
1174
v : typing .Any ,
1170
1175
exclude_readonly : bool ,
1171
- meta : typing .Optional [typing . Dict [str , typing .Any ]],
1176
+ meta : typing .Optional [dict [str , typing .Any ]],
1172
1177
) -> ET .Element :
1173
1178
wrapped_element = _create_xml_element (
1174
1179
meta .get ("name" ) if meta else None , meta .get ("prefix" ) if meta else None , meta .get ("ns" ) if meta else None
@@ -1211,7 +1216,7 @@ def _deserialize_xml(
1211
1216
def _convert_element (e : ET .Element ):
1212
1217
# dict case
1213
1218
if len (e .attrib ) > 0 or len ({child .tag for child in e }) > 1 :
1214
- dict_result : typing . Dict [str , typing .Any ] = {}
1219
+ dict_result : dict [str , typing .Any ] = {}
1215
1220
for child in e :
1216
1221
if dict_result .get (child .tag ) is not None :
1217
1222
if isinstance (dict_result [child .tag ], list ):
@@ -1224,7 +1229,7 @@ def _convert_element(e: ET.Element):
1224
1229
return dict_result
1225
1230
# array case
1226
1231
if len (e ) > 0 :
1227
- array_result : typing . List [typing .Any ] = []
1232
+ array_result : list [typing .Any ] = []
1228
1233
for child in e :
1229
1234
array_result .append (_convert_element (child ))
1230
1235
return array_result
0 commit comments