Support for Pydantic settings configuration file loading
Project description
Pydantic Config
Support for Pydantic settings configuration file loading
Installation
Pydantic Config can be installed via pip:
pip install pydantic-config
Pydantic Config is also available on conda under the conda-forge channel:
conda install pydantic-config -c conda-forge
Optional Dependencies
Pydantic-Config has the following optional dependencies:
- yaml -
pip install pydantic-config[yaml]
- toml -
pip install pydantic-config[toml]
Only for python<3.11
You can install all the optional dependencies with pip install pydantic-config[all]
Usage
# config.toml
app_name = "Python Application"
description = "Test application description"
from pydantic_config import SettingsModel, SettingsConfig
class Settings(SettingsModel):
app_id: str = 1
app_name: str = None
description: str = None
log_level: str = 'INFO'
model_config = SettingsConfig(
config_file='config.toml',
)
settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Test application description' log_level='INFO'
Using multiple config files
Multiple config files can be loaded by passing a list
of file names. Files will be loaded in the order they are listed.
Meaning later files in the list
will take priority over earlier files.
# config.toml
app_name = "Python Application"
description = "Test application description"
// config.json
{
"description": "Description from JSON file",
"log_level": "WARNING"
}
from pydantic_config import SettingsModel, SettingsConfig
class Settings(SettingsModel):
app_id: str = 1
app_name: str = 'App Name'
description: str = None
log_level: str = 'INFO'
model_config = SettingsConfig(
config_file=['config.toml', 'config.json'] # The config.json file will take priority over config.toml
)
settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Description from JSON file' log_level='WARNING'
Supported file formats
Currently, the following file formats are supported:
.yaml
Requirespyyaml
package.toml
Requirestomli
package for python<3.11.json
.ini
Requiring config files to load
Config files will attempt to be loaded from the specified file path. By default, if no file is found the file
will simply not be loaded (no error occurs). This may be useful if you want to specify config files that
may or may not exist. For example, you may have different config files for per
environment: config-prod.toml
and config-dev.toml
.
To disable this behavior set config_file_required=True
. This will cause an error to be raised
if the specified config file(s) do not exist. Setting this to True
will also prohibit the config_file
parameter from being set to None
or empty []
.
Merging
If your configurations have existing list
or dict
variables the contents will be merged by default. To disable
this behavior and override the contents instead you can set the config_merge
option to False
in the settings
Config
class.
# config.toml
[foo]
item1 = "value1"
# config2.toml
[foo]
item2 = "value2"
from pydantic_config import SettingsModel, SettingsConfig
class Settings(SettingsModel):
foo: dict = {}
model_config = SettingsConfig(
config_file=['config.toml', 'config2.toml'],
config_merge= True,
)
settings = Settings()
print(settings)
# foo={'item1': 'value1', 'item2': 'value2'}
# If config_merge=False then config2.toml would override the values from config.toml
# foo={'item2': 'value2'}
Duplicate items in merged lists
By default, all list
items will be merged into a single list regardless of duplicated items. To only keep
unique list items, set config_merge_unique=True
. This will only keep unique items in within a list.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic-config-0.3.0.tar.gz
.
File metadata
- Download URL: pydantic-config-0.3.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b59e4d2f2072d0e6a443fe3dbbb012bba04e972e5176a617540e33d0b1ff39c |
|
MD5 | e14790d5d3358c86c9a849d03c9a48a9 |
|
BLAKE2b-256 | 12ff1f38c845927142946d4c7b4a886b9ff3d7fde28e1d4d2c30dd624cd48e8a |
File details
Details for the file pydantic_config-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_config-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 572b737a527a372374e901169c6ac06d2f2766fca6ab12efaaf7046d1c504205 |
|
MD5 | 0e9ef87252f34892774a828dc308f396 |
|
BLAKE2b-256 | 721e104e05a072cc80149c95d127606f88bcb4114e2238be12511d2499e69bd3 |