Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_external_settings_v1-0.0.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_external_settings_v1-0.0.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_external_settings_v1-0.0.1.tar.gz.

File metadata

File hashes

Hashes for pydantic_external_settings_v1-0.0.1.tar.gz
Algorithm Hash digest
SHA256 47ac69858724e379bc58f58610bc21c6aed9c9257ba866f4129ff55de80b685e
MD5 8e73fde0cbfcb92c6f64ebef3848216c
BLAKE2b-256 73fc7a9d2f4d0390850bac7b4ab820289645ffb2d95f736e343984dd68d11756

See more details on using hashes here.

File details

Details for the file pydantic_external_settings_v1-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_external_settings_v1-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c21237092a27eeefd57cea6de7c098260333061646945dbe46b7910d0ddcd3cf
MD5 8866ed71b9d8ae21efc76d21b2bcd73e
BLAKE2b-256 58b7168a85f60e2f9b4de4e2d676458b5c05f5d477caa2cca8e2d9463f3d627b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page