in common/py_libs/config_spec.py [0:0]
def from_dict(cls: Type[T], config: dict) -> T:
"""Constructor that expects a dict loaded from a YAML file."""
logging.debug("Class: %s\n", cls.__name__)
# Retrieve the field type hints so that we can dynamically create
# appropriate nested types. An alternative way to retrieve field types
# would be to use dataclasses.fields(cls), however that approach doesn't
# resolve ForwardRefs for types that reference themselves.
field_types = typing.get_type_hints(cls)
attrs = {}
for key, value in config.items():
logging.debug("Key: %s", key)
logging.debug("Value: %s", value)
if key not in field_types:
raise cortex_exc.KeyCError(
f"{cls.__name__}.{key} is undefined or "
"missing type annotations.")
field_type = field_types[key]
logging.debug("field_type: %s", field_type)
possible_field_types = _unwrap_field_type(field_type)
if isinstance(value, list):
attrs[key] = [
_get_value_as_types(v, possible_field_types) for v in value
]
else:
attrs[key] = _get_value_as_types(value, possible_field_types)
return cls(**attrs)