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:
port:
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.0.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.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_settings_utils-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 cfcb8cf59a20cb096a04dfe47976028f2f6bc6190d096da046b0ff9ebe391312
MD5 737ab49c3d4138f91d5a5e2cd505b8fc
BLAKE2b-256 ea4698b30e0e3edb5c73d456b7c5cbd4c3f2ddc22a616d67259502adfd9a7536

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_settings_utils-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b22668ff9e141331203cece1e7559774354d482fc4492d2a6ac10dcacc8a539
MD5 b09032c7cfad26c9ae128548b3f2cead
BLAKE2b-256 1ace56da481e565af8557532d5bc8f5579acc64d26f9d2dc8832f57b60ce5bb6

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