11import datetime
22import logging
33import re
4+ from dateutil .parser import parse
45from typing import Any , ClassVar , Dict , Tuple
56
67import six
@@ -21,7 +22,7 @@ class ObjectMapper(object):
2122 PRIMITIVE_TYPES : ClassVar [Tuple [Any , ...]] = (float , bool , bytes , six .text_type , * six .integer_types )
2223 NATIVE_TYPES_MAPPING : ClassVar [Dict [str , Any ]] = {
2324 'int' : int ,
24- 'long' : int if six .PY3 else long , # noqa: F821, RUF100
25+ 'long' : int if six .PY3 else long , # noqa: F821, RUF100, YTT202
2526 'float' : float ,
2627 'str' : str ,
2728 'bool' : bool ,
@@ -31,39 +32,29 @@ class ObjectMapper(object):
3132 }
3233
3334 def to_json (self , obj ):
34-
3535 if obj is None :
3636 return None
3737 elif isinstance (obj , self .PRIMITIVE_TYPES ):
3838 return obj
3939 elif isinstance (obj , list ):
40- return [self .to_json (sub_obj )
41- for sub_obj in obj ]
40+ return [self .to_json (sub_obj ) for sub_obj in obj ]
4241 elif isinstance (obj , tuple ):
43- return tuple (self .to_json (sub_obj )
44- for sub_obj in obj )
42+ return tuple (self .to_json (sub_obj ) for sub_obj in obj )
4543 elif isinstance (obj , (datetime .datetime , datetime .date )):
4644 return obj .isoformat ()
47-
48- if isinstance (obj , dict ) or isinstance (obj , CaseInsensitiveDict ):
45+ elif isinstance (obj , dict ) or isinstance (obj , CaseInsensitiveDict ):
4946 obj_dict = obj
47+ elif hasattr (obj , 'attribute_map' ) and hasattr (obj , 'swagger_types' ):
48+ obj_dict = {obj .attribute_map [attr ]: getattr (obj , attr )
49+ for attr , _ in six .iteritems (obj .swagger_types )
50+ if getattr (obj , attr ) is not None }
5051 else :
51- # Convert model obj to dict except
52- # attributes `swagger_types`, `attribute_map`
53- # and attributes which value is not None.
54- # Convert attribute name to json key in
55- # model definition for request.
56- if hasattr (obj , 'attribute_map' ) and hasattr (obj , 'swagger_types' ):
57- obj_dict = {obj .attribute_map [attr ]: getattr (obj , attr )
58- for attr , _ in six .iteritems (obj .swagger_types )
59- if getattr (obj , attr ) is not None }
60- else :
61- obj_dict = {name : getattr (obj , name )
62- for name in vars (obj )
63- if getattr (obj , name ) is not None }
52+ obj_dict = {name : getattr (obj , name )
53+ for name in vars (obj )
54+ if getattr (obj , name ) is not None }
6455
6556 return {key : self .to_json (val )
66- for key , val in six .iteritems (obj_dict )}
57+ for key , val in six .iteritems (obj_dict )}
6758
6859 def from_json (self , data , klass ):
6960 return self .__deserialize (data , klass )
@@ -134,7 +125,6 @@ def __deserialize_date(self, string):
134125 :return: date.
135126 """
136127 try :
137- from dateutil .parser import parse
138128 return parse (string ).date ()
139129 except ImportError :
140130 return string
@@ -153,7 +143,6 @@ def __deserialize_datatime(self, string):
153143 :return: datetime.
154144 """
155145 try :
156- from dateutil .parser import parse
157146 return parse (string )
158147 except ImportError :
159148 return string
0 commit comments