in summarize_from_feedback/utils/hyperparams.py [0:0]
def _construct_from_json(ty, json_val, key=""):
"""
Construct a value of type `ty` based on the json value `json_val`.
"""
if json_val is None:
return json_val
if is_hparam_type(ty):
if isinstance(json_val, ty):
return json_val
if not isinstance(json_val, dict):
raise TypeError(
f"Tried to construct attribute {key} of type {ty} with value {json_val}"
)
x = ty()
x.override_from_json(json_val, key=key)
return x
if _is_list_type(ty):
subtype = ty.__args__[0]
return [_construct_from_json(subtype, y, key + ".listitem") for y in json_val]
if _is_dict_type(ty):
ktype = ty.__args__[0]
vtype = ty.__args__[1]
return {
_construct_from_json(ktype, k, key + ".dictkey"): _construct_from_json(
vtype, v, key + ".dictitem"
)
for k, v in json_val.items()
}
check_type(key, json_val, ty)
return json_val