loaders ¶
Custom settings sources for loading configuration from multiple file formats.
This module provides custom settings sources for pydantic-settings to load configuration from TOML, YAML, and JSON files with proper priority handling.
All loaders share common logic for field resolution and dictionary flattening, with only the file parsing implementation differing per format.
Classes:
-
TomlConfigSource–Load configuration from a TOML file.
-
YamlConfigSource–Load configuration from a YAML file.
-
JsonConfigSource–Load configuration from a JSON file.
Classes¶
FileConfigSource ¶
Bases: PydanticBaseSettingsSource, ABC
Abstract base class for file-based configuration sources.
Provides common functionality for loading configuration from files with different formats (TOML, YAML, JSON). Subclasses only need to implement the file parsing logic.
Initialize file config source.
Parameters:
-
settings_cls(type) –The settings class to load config for
-
config_file(Path) –Path to configuration file
Methods:
-
get_field_value–Get field value from configuration data.
-
__call__–Return all loaded config data.
Functions¶
_get_format_name ¶
_get_format_name() -> str
Get friendly format name for error messages.
Returns:
-
str–Format name (e.g., "TOML", "YAML", "JSON")
_parse_file abstractmethod ¶
get_field_value ¶
__call__ ¶
_flatten_nested_dict staticmethod ¶
Flatten nested dict with double underscore delimiter.
Converts nested dictionaries into flat dictionaries with keys joined by double underscores and uppercased, which matches pydantic-settings convention for case-insensitive field matching.
Parameters:
-
d(dict[str, Any]) –Dictionary to flatten
-
parent_key(str, default:'') –Parent key prefix, by default ""
Returns:
Examples:
>>> _flatten_nested_dict({"a": {"b": 1}})
{'A__B': 1}
>>> _flatten_nested_dict({"value_from_toml": "test"})
{'VALUE_FROM_TOML': 'test'}
TomlConfigSource ¶
Bases: FileConfigSource
Load configuration from a TOML file.
Initialize TOML config source.
Parameters:
-
settings_cls(type) –The settings class to load config for
-
config_file(Path, default:Path('config.toml')) –Path to TOML config file, by default Path("config.toml")
Methods:
-
get_field_value–Get field value from configuration data.
-
__call__–Return all loaded config data.
Functions¶
_get_format_name ¶
_get_format_name() -> str
Get friendly format name for error messages.
Returns:
-
str–Format name (e.g., "TOML", "YAML", "JSON")
get_field_value ¶
__call__ ¶
_flatten_nested_dict staticmethod ¶
Flatten nested dict with double underscore delimiter.
Converts nested dictionaries into flat dictionaries with keys joined by double underscores and uppercased, which matches pydantic-settings convention for case-insensitive field matching.
Parameters:
-
d(dict[str, Any]) –Dictionary to flatten
-
parent_key(str, default:'') –Parent key prefix, by default ""
Returns:
Examples:
>>> _flatten_nested_dict({"a": {"b": 1}})
{'A__B': 1}
>>> _flatten_nested_dict({"value_from_toml": "test"})
{'VALUE_FROM_TOML': 'test'}
YamlConfigSource ¶
Bases: FileConfigSource
Load configuration from a YAML file.
Initialize YAML config source.
Parameters:
-
settings_cls(type) –The settings class to load config for
-
config_file(Path, default:Path('config.yaml')) –Path to YAML config file, by default Path("config.yaml")
Methods:
-
get_field_value–Get field value from configuration data.
-
__call__–Return all loaded config data.
Functions¶
_get_format_name ¶
_get_format_name() -> str
Get friendly format name for error messages.
Returns:
-
str–Format name (e.g., "TOML", "YAML", "JSON")
get_field_value ¶
__call__ ¶
_flatten_nested_dict staticmethod ¶
Flatten nested dict with double underscore delimiter.
Converts nested dictionaries into flat dictionaries with keys joined by double underscores and uppercased, which matches pydantic-settings convention for case-insensitive field matching.
Parameters:
-
d(dict[str, Any]) –Dictionary to flatten
-
parent_key(str, default:'') –Parent key prefix, by default ""
Returns:
Examples:
>>> _flatten_nested_dict({"a": {"b": 1}})
{'A__B': 1}
>>> _flatten_nested_dict({"value_from_toml": "test"})
{'VALUE_FROM_TOML': 'test'}
JsonConfigSource ¶
Bases: FileConfigSource
Load configuration from a JSON file.
Initialize JSON config source.
Parameters:
-
settings_cls(type) –The settings class to load config for
-
config_file(Path, default:Path('config.json')) –Path to JSON config file, by default Path("config.json")
Methods:
-
get_field_value–Get field value from configuration data.
-
__call__–Return all loaded config data.
Functions¶
_get_format_name ¶
_get_format_name() -> str
Get friendly format name for error messages.
Returns:
-
str–Format name (e.g., "TOML", "YAML", "JSON")
get_field_value ¶
__call__ ¶
_flatten_nested_dict staticmethod ¶
Flatten nested dict with double underscore delimiter.
Converts nested dictionaries into flat dictionaries with keys joined by double underscores and uppercased, which matches pydantic-settings convention for case-insensitive field matching.
Parameters:
-
d(dict[str, Any]) –Dictionary to flatten
-
parent_key(str, default:'') –Parent key prefix, by default ""
Returns:
Examples:
>>> _flatten_nested_dict({"a": {"b": 1}})
{'A__B': 1}
>>> _flatten_nested_dict({"value_from_toml": "test"})
{'VALUE_FROM_TOML': 'test'}