provide a class descriptor for pydantic to load settings from external sources.compatible with pydantic 1.x
Project description
pydantic-external-settings-v1
This module provides a way to load settings from any external source, when using pydantic BaseSettings.
Quickstart
from pydantic_external_settings import ExternalSourceConfig
from pydantic import BaseSettings
from typing import ClassVar
class ApolloSourceConfig(ExternalSourceConfig):
def __init__(self, validation_alias: str = None, *, default_value=None):
super().__init__(self.get_from_apollo, validation_alias, default_value=default_value)
def get_from_apollo(self, key):
return key + "_***"
class S3Settings(BaseSettings):
api_key: str = "api_key"
bucket: ClassVar[str] = ApolloSourceConfig()
class Config:
env_file = '.env'
s3_settings = S3Settings()
print(s3_settings.bucket)
ExternalSourceConfig Usage
ExternalSourceConfig is a class descriptor, which can be used to load settings from any external source.
Note that the typing annotation is required to be ClassVar for the ExternalSourceConfig attribute, so that it will
not be considered as a field of the settings class. Currently, the ExternalSourceConfig supports to speacify a
validation_alias to override the name of setting field, and a default_value to provide a default value when the
settings field is not found both in the external source and the inner source(like .env file and environment variables).
To use a specific external usage, like database, you can create a subclass of ExternalSourceConfig
and provide a callable to the __init__ method, which will be called to get the value of the settings field. Note that
if
the field setting is not found in the external source, the callable should return None, so that the default value can be
used.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_external_settings_v1-0.0.1.tar.gz.
File metadata
- Download URL: pydantic_external_settings_v1-0.0.1.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47ac69858724e379bc58f58610bc21c6aed9c9257ba866f4129ff55de80b685e
|
|
| MD5 |
8e73fde0cbfcb92c6f64ebef3848216c
|
|
| BLAKE2b-256 |
73fc7a9d2f4d0390850bac7b4ab820289645ffb2d95f736e343984dd68d11756
|
File details
Details for the file pydantic_external_settings_v1-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_external_settings_v1-0.0.1-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c21237092a27eeefd57cea6de7c098260333061646945dbe46b7910d0ddcd3cf
|
|
| MD5 |
8866ed71b9d8ae21efc76d21b2bcd73e
|
|
| BLAKE2b-256 |
58b7168a85f60e2f9b4de4e2d676458b5c05f5d477caa2cca8e2d9463f3d627b
|