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

import sys

from pydantic_settings import CliSubCommand, get_subcommand

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

BaseSubprogramConfig = ConfigWithConfigFileField.factory("mysubprogram")


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

    duration: Duration


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


def example_config_cb(c: ExampleConfigCommand):
    argv = sys.argv
    # we patch sys.argv with an empty config file,
    # so AutomatorConfig does not search and load values
    # from default config files.
    sys.argv = [*sys.argv[:1], "-c", "/dev/null"]
    config = SubprogramConfig(
        duration="1d2h", **EXAMPLE_CONFIG_COMMANDS  # type: ignore
    ).model_dump_yaml(quiet=c.suppress_yaml_warning)
    sys.argv = argv
    return config


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.2.tar.gz (31.4 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.2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_settings_utils-0.1.2.tar.gz
  • Upload date:
  • Size: 31.4 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.2.tar.gz
Algorithm Hash digest
SHA256 54dcdf3e2f5372ffdaea70baad695551fc68c5cdbd3a8e5e2ba3905290acad66
MD5 030ee6c04fc507cd3d25736c73b2dd0e
BLAKE2b-256 df447706c89499d195354df8652e19698fbde1a1d69bdbfb61fcc8bde57b2656

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_settings_utils-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e3a3ec79677cddeb325400a6bfa451ae7d6a5f7459d8a6fe64bac9cded7d6290
MD5 3bc8fa7523c57df815fd9ce1d299f723
BLAKE2b-256 1beadce250c6dc4ba9b0bf10a34b7fd0ed383977e9dd804f95c1b4a5aa16633e

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