@@ -108,8 +108,13 @@ def parse_value(
108
108
value_type : Any ,
109
109
default : Any = MISSING ,
110
110
allow_none : bool = True ,
111
+ allow_sdk_types : bool = False ,
111
112
) -> Any :
112
- """Try to parse a value from raw (json) data and type annotations."""
113
+ """
114
+ Try to parse a value from raw (json) data and type annotations.
115
+
116
+ If allow_sdk_types is False, any SDK specific custom data types will be converted.
117
+ """
113
118
# pylint: disable=too-many-return-statements,too-many-branches
114
119
115
120
if isinstance (value_type , str ):
@@ -131,9 +136,9 @@ def parse_value(
131
136
return default
132
137
if value is None and value_type is NoneType :
133
138
return None
134
- if value is None and allow_none :
135
- return None
136
139
if value is None and value_type is Nullable :
140
+ return Nullable () if allow_sdk_types else None
141
+ if value is None and allow_none :
137
142
return None
138
143
if is_dataclass (value_type ) and isinstance (value , dict ):
139
144
return dataclass_from_dict (value_type , value )
@@ -220,12 +225,12 @@ def parse_value(
220
225
if value_type is uint and (
221
226
isinstance (value , int ) or (isinstance (value , str ) and value .isnumeric ())
222
227
):
223
- return uint (value )
228
+ return uint (value ) if allow_sdk_types else int ( value )
224
229
if value_type is float32 and (
225
230
isinstance (value , (float , int ))
226
231
or (isinstance (value , str ) and value .isnumeric ())
227
232
):
228
- return float32 (value )
233
+ return float32 (value ) if allow_sdk_types else float ( value )
229
234
230
235
# If we reach this point, we could not match the value with the type and we raise
231
236
if not isinstance (value , value_type ):
@@ -236,7 +241,9 @@ def parse_value(
236
241
return value
237
242
238
243
239
- def dataclass_from_dict (cls : type [_T ], dict_obj : dict , strict : bool = False ) -> _T :
244
+ def dataclass_from_dict (
245
+ cls : type [_T ], dict_obj : dict , strict : bool = False , allow_sdk_types : bool = False
246
+ ) -> _T :
240
247
"""
241
248
Create (instance of) a dataclass by providing a dict with values.
242
249
@@ -259,6 +266,7 @@ def dataclass_from_dict(cls: type[_T], dict_obj: dict, strict: bool = False) ->
259
266
type_hints [field .name ],
260
267
field .default ,
261
268
allow_none = not strict ,
269
+ allow_sdk_types = allow_sdk_types ,
262
270
)
263
271
for field in dc_fields
264
272
if field .init
0 commit comments