Skip to main content

Hipster-orgazmic tool to mange application settings

Project description

Hipster-orgazmic tool to mange application settings

https://travis-ci.com/danields761/pydantic-settings.svg?branch=master https://badge.fury.io/py/pydantic-settings.svg https://readthedocs.org/projects/pydantic-settings/badge/?version=latest

Library which extends pydantic functionality in scope of application settings. Pydantic already have settings implementation, e.g. pydantic.BaseSettings, but from my point it’s missing some useful features:

  1. Overriding settings values by environment variables even for nested fields

  2. Providing detailed information about value location inside a loaded file or environment variable, which helps to point user mistake

  3. Documenting model fields isn’t feels comfortable, but it’s really essential to write comprehensive documentation for application settings

NOTE: Beta quality

Installation

Using pip:

pip install pydantic-settings

Usage example

Override values by env variables

Allows to override values for nested fields if they are represented as pydantic model.

Here is example:

from pydantic import BaseModel
from pydantic_settings import BaseSettingsModel, load_settings


class ComponentOptions(BaseModel):
    val: str


class AppSettings(BaseSettingsModel):
    class Config:
        env_prefix = 'FOO'

    component: ComponentOptions


assert (
    load_settings(
        AppSettings,
        '{}',
        load_env=True,
        type_hint='json',
        environ={'FOO_COMPONENT_VAL': 'SOME VALUE'},
    ).component.val
    == 'SOME VALUE'
)

Point exact error location inside file

from pydantic import ValidationError, IntegerError
from pydantic_settings import BaseSettingsModel, load_settings, TextLocation
from pydantic_settings.errors import ExtendedErrorWrapper


class Foo(BaseSettingsModel):
    val: int


try:
    load_settings(Foo, '{"val": "NOT AN INT"}', type_hint='json')
except ValidationError as e:
    err_wrapper, *_ = e.raw_errors
    assert isinstance(err_wrapper, ExtendedErrorWrapper)
    assert isinstance(err_wrapper.exc, IntegerError)
    assert err_wrapper.source_loc == TextLocation(
        line=1, col=9, end_line=1, end_col=21, pos=9, end_pos=20
    )
else:
    raise Exception('must rise error')

Extracts fields documentation

Allows to extract Sphinx style attributes documentation by processing AST tree of class definition

from pydantic import BaseModel
from pydantic_settings import with_attrs_docs


@with_attrs_docs
class Foo(BaseModel):
    bar: str
    """here is docs"""

    #: docs for baz
    baz: int

    #: yes
    #: of course
    is_there_multiline: bool = True


assert Foo.__fields__['bar'].field_info.description == 'here is docs'
assert Foo.__fields__['baz'].field_info.description == 'docs for baz'
assert Foo.__fields__['is_there_multiline'].field_info.description == (
    'yes\nof course'
)

Online docs

Read more detailed documentation on the project Read The Docs page.

Development setup

Project requires poetry for development setup.

  • If you aren’t have it already

pip install poetry
  • Install project dependencies

poetry install
  • Run tests

poetry run pytest .
  • Great, all works! Expect one optional step:

  • Install pre-commit for pre-commit hooks

pip install pre-commit
pre-commit install

That will install pre-commit hooks, which will check code with flake8 and black.

NOTE project uses black as code formatter, but i’am personally really dislike their “double quoted strings everywhere” style, that’s why black -S should be used (anyway it’s configured in pyproject.toml file)

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-settings-0.2.5.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

pydantic_settings-0.2.5-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-settings-0.2.5.tar.gz.

File metadata

  • Download URL: pydantic-settings-0.2.5.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Linux/5.4.0-31-generic

File hashes

Hashes for pydantic-settings-0.2.5.tar.gz
Algorithm Hash digest
SHA256 a0657303cd61a4ff1f43a04053032379ee8e8a9559998863da59144840590e70
MD5 1bb14f38beafcb68da39730dd07a87fc
BLAKE2b-256 d40d5eb89b2c1fc0da90c7726d86ace4996fc8b916409fa4b40aff97158c3d19

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_settings-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: pydantic_settings-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Linux/5.4.0-31-generic

File hashes

Hashes for pydantic_settings-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 347991a2d96674dc9a412b6306d2fa1d84605e16d5180b04763ec4280116dbee
MD5 14ad3bbf16c0ad2c502323c5f928c00b
BLAKE2b-256 ab8eec99b1a4731fc4dc0aa683f6ca8d124a17206883f67ca2eeeb4fa98f9f78

See more details on using hashes here.

Provenance

Supported by

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