@@ -346,7 +346,7 @@ def _get_model(module_name: str, model_name: str):
346
346
347
347
348
348
class _MyMutableMapping (MutableMapping [str , typing .Any ]):
349
- def __init__ (self , data : typing . Dict [str , typing .Any ]) -> None :
349
+ def __init__ (self , data : dict [str , typing .Any ]) -> None :
350
350
self ._data = data
351
351
352
352
def __contains__ (self , key : typing .Any ) -> bool :
@@ -426,7 +426,7 @@ def pop(self, key: str, default: typing.Any = _UNSET) -> typing.Any:
426
426
return self ._data .pop (key )
427
427
return self ._data .pop (key , default )
428
428
429
- def popitem (self ) -> typing . Tuple [str , typing .Any ]:
429
+ def popitem (self ) -> tuple [str , typing .Any ]:
430
430
"""
431
431
Removes and returns some (key, value) pair
432
432
:returns: The (key, value) pair.
@@ -514,9 +514,7 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
514
514
return o
515
515
516
516
517
- def _get_rest_field (
518
- attr_to_rest_field : typing .Dict [str , "_RestField" ], rest_name : str
519
- ) -> typing .Optional ["_RestField" ]:
517
+ def _get_rest_field (attr_to_rest_field : dict [str , "_RestField" ], rest_name : str ) -> typing .Optional ["_RestField" ]:
520
518
try :
521
519
return next (rf for rf in attr_to_rest_field .values () if rf ._rest_name == rest_name )
522
520
except StopIteration :
@@ -539,7 +537,7 @@ class Model(_MyMutableMapping):
539
537
_is_model = True
540
538
# label whether current class's _attr_to_rest_field has been calculated
541
539
# could not see _attr_to_rest_field directly because subclass inherits it from parent class
542
- _calculated : typing . Set [str ] = set ()
540
+ _calculated : set [str ] = set ()
543
541
544
542
def __init__ (self , * args : typing .Any , ** kwargs : typing .Any ) -> None :
545
543
class_name = self .__class__ .__name__
@@ -624,7 +622,7 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self:
624
622
# we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping',
625
623
# 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object'
626
624
mros = cls .__mro__ [:- 9 ][::- 1 ] # ignore parents, and reverse the mro order
627
- 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
628
626
k : v for mro_class in mros for k , v in mro_class .__dict__ .items () if k [0 ] != "_" and hasattr (v , "_type" )
629
627
}
630
628
annotations = {
@@ -639,7 +637,7 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self:
639
637
rf ._type = rf ._get_deserialize_callable_from_annotation (annotations .get (attr , None ))
640
638
if not rf ._rest_name_input :
641
639
rf ._rest_name_input = attr
642
- 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 ())
643
641
cls ._calculated .add (f"{ cls .__module__ } .{ cls .__qualname__ } " )
644
642
645
643
return super ().__new__ (cls )
@@ -681,7 +679,7 @@ def _deserialize(cls, data, exist_discriminators):
681
679
mapped_cls = cls .__mapping__ .get (discriminator_value , cls ) # pyright: ignore # pylint: disable=no-member
682
680
return mapped_cls ._deserialize (data , exist_discriminators )
683
681
684
- 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 ]:
685
683
"""Return a dict that can be turned into json using json.dump.
686
684
687
685
:keyword bool exclude_readonly: Whether to remove the readonly properties.
@@ -741,7 +739,7 @@ def _deserialize_with_union(deserializers, obj):
741
739
def _deserialize_dict (
742
740
value_deserializer : typing .Optional [typing .Callable ],
743
741
module : typing .Optional [str ],
744
- obj : typing . Dict [typing .Any , typing .Any ],
742
+ obj : dict [typing .Any , typing .Any ],
745
743
):
746
744
if obj is None :
747
745
return obj
@@ -751,7 +749,7 @@ def _deserialize_dict(
751
749
752
750
753
751
def _deserialize_multiple_sequence (
754
- entry_deserializers : typing . List [typing .Optional [typing .Callable ]],
752
+ entry_deserializers : list [typing .Optional [typing .Callable ]],
755
753
module : typing .Optional [str ],
756
754
obj ,
757
755
):
@@ -772,14 +770,14 @@ def _deserialize_sequence(
772
770
return type (obj )(_deserialize (deserializer , entry , module ) for entry in obj )
773
771
774
772
775
- def _sorted_annotations (types : typing . List [typing .Any ]) -> typing . List [typing .Any ]:
773
+ def _sorted_annotations (types : list [typing .Any ]) -> list [typing .Any ]:
776
774
return sorted (
777
775
types ,
778
776
key = lambda x : hasattr (x , "__name__" ) and x .__name__ .lower () in ("str" , "float" , "int" , "bool" ),
779
777
)
780
778
781
779
782
- 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
783
781
annotation : typing .Any ,
784
782
module : typing .Optional [str ],
785
783
rf : typing .Optional ["_RestField" ] = None ,
@@ -844,7 +842,10 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur
844
842
return functools .partial (_deserialize_with_union , deserializers )
845
843
846
844
try :
847
- 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" :
848
849
value_deserializer = _get_deserialize_callable_from_annotation (
849
850
annotation .__args__ [1 ], module , rf # pyright: ignore
850
851
)
@@ -857,7 +858,10 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur
857
858
except (AttributeError , IndexError ):
858
859
pass
859
860
try :
860
- 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" ]:
861
865
if len (annotation .__args__ ) > 1 : # pyright: ignore
862
866
entry_deserializers = [
863
867
_get_deserialize_callable_from_annotation (dt , module , rf )
@@ -975,11 +979,11 @@ def __init__(
975
979
name : typing .Optional [str ] = None ,
976
980
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
977
981
is_discriminator : bool = False ,
978
- visibility : typing .Optional [typing . List [str ]] = None ,
982
+ visibility : typing .Optional [list [str ]] = None ,
979
983
default : typing .Any = _UNSET ,
980
984
format : typing .Optional [str ] = None ,
981
985
is_multipart_file_input : bool = False ,
982
- xml : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
986
+ xml : typing .Optional [dict [str , typing .Any ]] = None ,
983
987
):
984
988
self ._type = type
985
989
self ._rest_name_input = name
@@ -1037,11 +1041,11 @@ def rest_field(
1037
1041
* ,
1038
1042
name : typing .Optional [str ] = None ,
1039
1043
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
1040
- visibility : typing .Optional [typing . List [str ]] = None ,
1044
+ visibility : typing .Optional [list [str ]] = None ,
1041
1045
default : typing .Any = _UNSET ,
1042
1046
format : typing .Optional [str ] = None ,
1043
1047
is_multipart_file_input : bool = False ,
1044
- xml : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
1048
+ xml : typing .Optional [dict [str , typing .Any ]] = None ,
1045
1049
) -> typing .Any :
1046
1050
return _RestField (
1047
1051
name = name ,
@@ -1058,8 +1062,8 @@ def rest_discriminator(
1058
1062
* ,
1059
1063
name : typing .Optional [str ] = None ,
1060
1064
type : typing .Optional [typing .Callable ] = None , # pylint: disable=redefined-builtin
1061
- visibility : typing .Optional [typing . List [str ]] = None ,
1062
- 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 ,
1063
1067
) -> typing .Any :
1064
1068
return _RestField (name = name , type = type , is_discriminator = True , visibility = visibility , xml = xml )
1065
1069
@@ -1078,9 +1082,9 @@ def serialize_xml(model: Model, exclude_readonly: bool = False) -> str:
1078
1082
def _get_element (
1079
1083
o : typing .Any ,
1080
1084
exclude_readonly : bool = False ,
1081
- parent_meta : typing .Optional [typing . Dict [str , typing .Any ]] = None ,
1085
+ parent_meta : typing .Optional [dict [str , typing .Any ]] = None ,
1082
1086
wrapped_element : typing .Optional [ET .Element ] = None ,
1083
- ) -> typing .Union [ET .Element , typing . List [ET .Element ]]:
1087
+ ) -> typing .Union [ET .Element , list [ET .Element ]]:
1084
1088
if _is_model (o ):
1085
1089
model_meta = getattr (o , "_xml" , {})
1086
1090
@@ -1169,7 +1173,7 @@ def _get_element(
1169
1173
def _get_wrapped_element (
1170
1174
v : typing .Any ,
1171
1175
exclude_readonly : bool ,
1172
- meta : typing .Optional [typing . Dict [str , typing .Any ]],
1176
+ meta : typing .Optional [dict [str , typing .Any ]],
1173
1177
) -> ET .Element :
1174
1178
wrapped_element = _create_xml_element (
1175
1179
meta .get ("name" ) if meta else None , meta .get ("prefix" ) if meta else None , meta .get ("ns" ) if meta else None
@@ -1212,7 +1216,7 @@ def _deserialize_xml(
1212
1216
def _convert_element (e : ET .Element ):
1213
1217
# dict case
1214
1218
if len (e .attrib ) > 0 or len ({child .tag for child in e }) > 1 :
1215
- dict_result : typing . Dict [str , typing .Any ] = {}
1219
+ dict_result : dict [str , typing .Any ] = {}
1216
1220
for child in e :
1217
1221
if dict_result .get (child .tag ) is not None :
1218
1222
if isinstance (dict_result [child .tag ], list ):
@@ -1225,7 +1229,7 @@ def _convert_element(e: ET.Element):
1225
1229
return dict_result
1226
1230
# array case
1227
1231
if len (e ) > 0 :
1228
- array_result : typing . List [typing .Any ] = []
1232
+ array_result : list [typing .Any ] = []
1229
1233
for child in e :
1230
1234
array_result .append (_convert_element (child ))
1231
1235
return array_result
0 commit comments