Skip to main content

Hipster-orgazmic tool to mange application settings

Project description

Pydantic settings

Hipster-orgazmic tool to mange application settings

Build Status PyPI version Documentation Status

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: Alpha 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, ValidationError
from pydantic_settings import BaseSettingsModel

class Nested(BaseModel):
    foo: int

class Settings(BaseSettingsModel):
    nested: Nested


try:
    Settings.from_env({'APP_nested_FOO': 'NOT AN INT'})
except ValidationError as e:
    assert e.raw_errors[0].env_loc == 'APP_nested_FOO'  # shows exact env variable name

Point exact error location inside file

from pydantic import BaseModel, IntegerError
from pydantic_settings import BaseSettingsModel, LoadingValidationError, load_settings, FileLocation

class Nested(BaseModel):
    foo: int

class Settings(BaseSettingsModel):
    nested: Nested

conf_text = """
nested:
    foo: 'NOT AN INT'
"""

try:
    load_settings(Settings, conf_text, type_hint='yaml')
except LoadingValidationError as e:
    assert e.raw_errors[0].loc == ('nested', 'foo')
    assert e.raw_errors[0].text_loc == FileLocation(line=3, col=10, end_line=3, end_col=22)
    assert isinstance(e.raw_errors[0].exc, IntegerError)

Extracts fields documentation

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

from pydantic_settings import BaseSettingsModel

class Foo(BaseSettingsModel):
    class Config:
        build_attr_docs = True

    bar: str
    """here is docs"""

    #: this style is't supported, but probably will be added in future
    baz: int

assert Foo.__fields__['bar'].schema.description == 'here is docs'
assert Foo.__fields__['baz'].schema.description is None  # :(

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.1.1b0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

pydantic_settings-0.1.1b0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-settings-0.1.1b0.tar.gz.

File metadata

  • Download URL: pydantic-settings-0.1.1b0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0a5 CPython/3.7.3 Linux/5.0.0-21-generic

File hashes

Hashes for pydantic-settings-0.1.1b0.tar.gz
Algorithm Hash digest
SHA256 6fa0133bb988d73d5eaae426b53e15ed116960ebb5a50c99bd7e4e2b188b75ea
MD5 77c81de439d9617440a44a47b78d8844
BLAKE2b-256 f583482563502c75acf7e7b2cb9662c400e6d8e5c691f8e9596c3b6136cd7917

See more details on using hashes here.

File details

Details for the file pydantic_settings-0.1.1b0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_settings-0.1.1b0-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0a5 CPython/3.7.3 Linux/5.0.0-21-generic

File hashes

Hashes for pydantic_settings-0.1.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed26a09d297f0e7d8c6aae0b0c94a446157fe43cdbfe17ca722ee1cae1f09a3d
MD5 a6fc6ee2786c4a7d1137a803974dac2e
BLAKE2b-256 3ad1cb3b20c505591d014e59db313ad99460b52ffeace5daf4a066451146e8ec

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