def fill_defaults(schema, args):
"""DEPRECATED, function to fill in default values from schema into args
bug: goes into an infinite loop when there is a recursively defined schema
Parameters
----------
schema : marshmallow.Schema
schema to get defaults from
args :
Returns
-------
dict
dictionary with missing default values filled in
"""
defaults = []
# find all of the schema entries with default values
schemata = [(schema, [])]
while schemata:
subschema, path = schemata.pop()
for k, v in subschema.declared_fields.items():
if isinstance(v, mm.fields.Nested):
schemata.append((v.schema, path + [k]))
elif v.default != mm.missing:
defaults.append((path + [k], v.default))
# put the default entries into the args dictionary
args = copy.deepcopy(args)
for path, val in defaults:
d = args
for path_item in path[:-1]:
d = d.setdefault(path_item, {})
if path[-1] not in d:
d[path[-1]] = val
return args
评论列表
文章目录