@@ -29,7 +29,8 @@ class JsonAdapter(object):
29
29
_PREFIX_KEY_PROTECTED = '_'
30
30
31
31
# Constants to fetch param types from the docstrings
32
- _PATTERN_PARAM_TYPES = ':type (_?{}): ([\w.]+)(?:\[([\w.]+)\])?'
32
+ _TEMPLATE_PATTERN_PARAM_TYPES = ':type (_?{}): ([\w.]+)(?:\[([\w.]+)\])?'
33
+ _PATTERN_PARAM_NAME_TYPED_ANY = ':type (\w+):'
33
34
_SUBMATCH_INDEX_NAME = 1
34
35
_SUBMATCH_INDEX_TYPE_MAIN = 2
35
36
_SUBMATCH_INDEX_TYPE_SUB = 3
@@ -163,7 +164,9 @@ def _deserialize_dict(cls, cls_target, dict_):
163
164
"""
164
165
165
166
instance = cls_target .__new__ (cls_target , cls_target )
166
- instance .__dict__ = cls ._deserialize_dict_attributes (cls_target , dict_ )
167
+ dict_deserialized = cls ._deserialize_dict_attributes (cls_target , dict_ )
168
+ instance .__dict__ = cls ._set_default_values (cls_target ,
169
+ dict_deserialized )
167
170
168
171
return instance
169
172
@@ -192,18 +195,6 @@ def _deserialize_dict_attributes(cls, cls_context, dict_):
192
195
193
196
return dict_deserialized
194
197
195
- @classmethod
196
- def _warn_key_unknown (cls , cls_context , key ):
197
- """
198
- :type cls_context: type
199
- :type key: str
200
-
201
- :rtype: None
202
- """
203
-
204
- context_name = cls_context .__name__
205
- warnings .warn (cls ._WARNING_KEY_UNKNOWN .format (key , context_name ))
206
-
207
198
@classmethod
208
199
def _deserialize_key (cls , key ):
209
200
"""
@@ -240,7 +231,7 @@ def _fetch_attribute_specs_from_doc(cls, cls_in, attribute_name):
240
231
:rtype: ValueSpecs
241
232
"""
242
233
243
- pattern = cls ._PATTERN_PARAM_TYPES .format (attribute_name )
234
+ pattern = cls ._TEMPLATE_PATTERN_PARAM_TYPES .format (attribute_name )
244
235
match = re .search (pattern , cls_in .__doc__ )
245
236
246
237
if match is not None :
@@ -366,6 +357,37 @@ def _deserialize_list(cls, type_item, list_):
366
357
367
358
return list_deserialized
368
359
360
+ @classmethod
361
+ def _warn_key_unknown (cls , cls_context , key ):
362
+ """
363
+ :type cls_context: type
364
+ :type key: str
365
+
366
+ :rtype: None
367
+ """
368
+
369
+ context_name = cls_context .__name__
370
+ warnings .warn (cls ._WARNING_KEY_UNKNOWN .format (key , context_name ))
371
+
372
+ @classmethod
373
+ def _set_default_values (cls , cls_context , dict_ ):
374
+ """
375
+ :type cls_context: type
376
+ :type dict_: dict
377
+
378
+ :rtype: dict
379
+ """
380
+
381
+ dict_with_default_values = dict_ .copy ()
382
+ params = re .findall (cls ._PATTERN_PARAM_NAME_TYPED_ANY ,
383
+ cls_context .__doc__ )
384
+
385
+ for param in params :
386
+ if param not in dict_with_default_values :
387
+ dict_with_default_values [param ] = None
388
+
389
+ return dict_with_default_values
390
+
369
391
@classmethod
370
392
def can_serialize (cls ):
371
393
"""
0 commit comments