Skip to main content

Use [pydantic](https://docs.pydantic.dev/latest/) to manage persistent config for your [Typer](https://typer.tiangolo.com/) CLI apps!

Project description

Typer Pydantic Config

This package helps you quickly build python CLI applications with a persistent config using typer & pydantic.

minimal_example.gif

See minimal.py and example.py for two simple examples.

Installation

Install it from pypi using your preferred method, e.g. through uv:

uv pip install typer-pydantic-config

Usage

  1. Implement config object as pydantic class
  2. Use get_config where every you need the config object
  3. Build your typer app
  4. Start the app with start_config_app(app, <YourConfigPydanticClass>)

On the first invocation, prompts the user to set all values in the config file.

Your app now has an additional config command with the following signature:

$ python example.py config --help
Usage: example.py config [OPTIONS] COMMAND [ARGS]...

  Interact with config: (delete | init | path | set | show).

Options:
  --help  Show this message and exit.

Commands:
  delete  Delete config file on disk.
  init    Interactively prompt for every field in the config.
  path    Print config path.
  set     Set one or more config fields via flags.
  show    Print content of config file.

⚠ Current Limitations ⚠

  • The package is still an early draft and not yet thoroughly tested
  • The following types are supported as attributes of your config class:
    • int
    • float
    • bool
    • str
    • datetime
    • Path
    • another subclass of pydantic's BaseModel
  • Config values are available as ctx.obj["config"] (see click docs: Context.
    • This is only available when one of the endpoint functions is called.
    • Do not overwrite the context.obj (at least not the "config" key.
  • Unique app name required:
    • Set a unique name: app = typer.Typer(name="<some_unique_name>")
    • This name is used by platformdirs to construct the path of the config.

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

typer_pydantic_config-0.3.3.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

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

typer_pydantic_config-0.3.3-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file typer_pydantic_config-0.3.3.tar.gz.

File metadata

File hashes

Hashes for typer_pydantic_config-0.3.3.tar.gz
Algorithm Hash digest
SHA256 6a575fa2cc499235a593c2d7b10b0aaac4ce7c3df360468ef1454f0bca692b65
MD5 7416b3fde61dede9d9c565eec8e1a0dd
BLAKE2b-256 f1cdd0c3d2f94667797b63c3763bb91794e18a3e445f4bb56e9faedbf9743f64

See more details on using hashes here.

File details

Details for the file typer_pydantic_config-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for typer_pydantic_config-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2142d5803ad04365e1928e21f7a7b122f535d759ac0b4532f4b5a3037c3d6baf
MD5 62a655f814aebdb912d5ce9fc7a5326f
BLAKE2b-256 ae347a88bb036643dc881b4a6702a00eb599ecb7e4c542754823a699d7bfc587

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