Skip to content

Construct config

Loads values from the sources passed in (in order) and creates an instance of the config schema specified.

Parameters:

Name Type Description Default
config_schema Type[~ConfigClass]

a subclass of ConfigSchema (or Pydantic's BaseModel) to validate config values with

required
sources Union[Sequence[Union[flex_config.config_source.ConfigSource, Callable[[Dict[str, Any]], flex_config.config_source.ConfigSource]]], flex_config.config_source.ConfigSource, Callable[[Dict[str, Any]], flex_config.config_source.ConfigSource]]

A list of ConfigSources to be loaded and merged in the order they were passed in

required

Returns:

Type Description
~ConfigClass

An instance of the config schema passed in containing values loaded (in order) from the sources passed in

Exceptions:

Type Description
TypeError

If config_schema passed in is not a subclass of ConfigSchema (which is just a renamed and re-exported pydantic.BaseModel which you could subclass instead if you already have it imported)

ValidationError

If there are issues validating the compiled config values e.g. missing values or values that couldn't be converted to their specified type

Source code in flex_config\__init__.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def construct_config(
    config_schema: Type[ConfigClass], sources: Union[Sequence[_SourceTypes], _SourceTypes]
) -> ConfigClass:
    """Loads values from the sources passed in (in order) and creates an instance of the config schema specified.

    Args:
        config_schema: a subclass of ConfigSchema (or Pydantic's BaseModel) to validate config values with
        sources: A list of ConfigSources to be loaded and merged in the order they were passed in

    Returns:
        An instance of the config schema passed in containing values loaded (in order) from the sources passed in

    Raises:
        TypeError: If config_schema passed in is not a subclass of ConfigSchema (which is just a renamed and re-exported
            [pydantic.BaseModel](https://pydantic-docs.helpmanual.io/usage/models/#basic-model-usage) which you could
            subclass instead if you already have it imported)
        ValidationError: If there are issues validating the compiled config values e.g. missing values or values that
            couldn't be converted to their specified type
    """
    if not issubclass(config_schema, ConfigSchema):
        raise TypeError("Config schema supplied isn't a subclass of ConfigSchema (aka Pydantic's BaseModel)")
    compiled_config_dict = _compile_sources(sources=sources)
    return cast(ConfigClass, config_schema(**compiled_config_dict))