Skip to main content

pydantic-settings-utils

Usage

from pydantic_settings_utils import (
    EXAMPLE_CONFIG_COMMANDS,
    ConfigWithConfigFileField,
    Duration,
)

BaseConfig = ConfigWithConfigFileField.factory("myapp")


class Config(BaseConfig):
    host: str = "localhost"
    port: int = 8080

    duration: Duration


EXAMPLE_CONFIG = Config(duration="1d2h", **EXAMPLE_CONFIG_COMMANDS)  # type: ignore
example_config_cb = lambda c: EXAMPLE_CONFIG.model_dump_yaml(
    quiet=c.suppress_yaml_warning
)

config = Config.init(example_config_cb=example_config_cb)

print(config.model_dump_json(indent=2))
$ uv run examples/basic.py -h
usage: basic.py [-h] [-c str] [-v] [--host str] [--port int]
                [--duration timedelta]
                {current-config,example-config} ...

options:
  -h, --help            show this help message and exit
  -c, --config str      path to config file (.y[a]ml). Defaults to
                        /home/example/.config/myapp/config.y[a]ml or
                        /etc/myapp/config.y[a]ml if unspecified. (default:
                        None)
  -v, --verbose
  --host str            (default: localhost)
  --port int            (default: 8080)
  --duration timedelta  (required)

subcommands:
  {current-config,example-config}
    current-config
    example-config

Subcommand: example-config

$ uv run examples/basic.py example-config
---
host: localhost
port: 8080
duration: 1d2h

Subcommand: current-config

$ uv run examples/basic.py --duration 1d current-config
---
host: localhost
port: 8080
duration: 1d

YAML config file

$ cat examples/basic.yaml
---
host: example.com
port: 1024
duration: 1d

$ uv run examples/basic.py -c examples/basic.yaml current-config
---
host: example.com
port: 1024
duration: 1d

Subprograms

from pydantic_settings import CliSubCommand, get_subcommand

from pydantic_settings_utils import (
    EXAMPLE_CONFIG_COMMANDS,
    ConfigBase,
    ConfigWithConfigFileField,
    Duration,
    StandaloneConfig,
    WithRelaxedSubcommands,
)

BaseSubprogramConfig = ConfigWithConfigFileField.factory("mysubprogram")


class SubprogramConfig(BaseSubprogramConfig):
    host: str = "localhost"
    port: int = 8080

    duration: Duration


class Config(ConfigBase):
    subprogram: CliSubCommand[SubprogramConfig]


EXAMPLE_SUBPROGRAM_CONFIG = SubprogramConfig(duration="1d2h", **EXAMPLE_CONFIG_COMMANDS)  # type: ignore
example_config_cb = lambda c: EXAMPLE_SUBPROGRAM_CONFIG.model_dump_yaml(
    quiet=c.suppress_yaml_warning
)


def subprogram(config: SubprogramConfig | None = None):
    config = SubprogramConfig.init(config, example_config_cb=example_config_cb)
    print(config.model_dump_json(indent=2))


config = StandaloneConfig(WithRelaxedSubcommands(Config)).load(
    ignore_subcommand_help_flags=True
)
match get_subcommand(config):
    case SubprogramConfig() as c:
        subprogram(c)
$ uv run examples/subprogram.py -h
usage: subprogram.py [-h] {subprogram} ...

options:
  -h, --help    show this help message and exit

subcommands:
  {subprogram}
    subprogram

$ uv run examples/subprogram.py subprogram -h
usage: subprogram.py [-h] [-c str] [-v] [--host str] [--port int]
                     [--duration timedelta]
                     {current-config,example-config} ...

...

Development

Enable git hooks to format staged code and generate README pre-commit:

git config core.hooksPath .githooks

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_utils-0.1.1.tar.gz (31.1 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_utils-0.1.1-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_settings_utils-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic_settings_utils-0.1.1.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_settings_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d3ad1c8646354a9e2e97f13f80449c9a3d332bfb5b9c4c0d6f90d504315b9cff
MD5 8c6aa21ccc0303c18f86feacc77ad7af
BLAKE2b-256 bc27f0b5436bf596f5fc1290b8c6c8b023a870154df2bbda31cd35a2c72c324f

See more details on using hashes here.

File details

Details for the file pydantic_settings_utils-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_settings_utils-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_settings_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75f7a94cf620f44ce76fd9eeb7b2bc8ac76c2a3280b5343c8e4c09d82457eb81
MD5 590f1278f221e6b82b958301f16b9506
BLAKE2b-256 582ef2bfd0a0256a3ccf81498901159f615ad45de35048e389a859cd1fae0886

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