Auto-generate Typer CLI interfaces from Pydantic models.
Project description
typantic
Auto-generate Typer CLI interfaces from Pydantic models.
Define your config once as a Pydantic model with validators, and get a fully-typed CLI for free — no duplication, no drift.
Installation
pip install typantic
Quick start
from pathlib import Path
from typing import Annotated
import typer
from pydantic import AfterValidator, BaseModel, Field
from typantic import pydantic_to_typer
# 1. Define your config with validators
class Config(BaseModel):
images: Annotated[
list[Path],
Field(description="Image folders to process.", kw_only=False),
]
output: Annotated[
Path,
AfterValidator(Path.resolve),
Field(description="Output directory.", kw_only=True),
]
threshold: Annotated[
float,
Field(default=0.5, description="Detection threshold.", kw_only=True),
]
seed: Annotated[
int | None,
Field(default=None, description="Random seed.", kw_only=True),
]
# 2. Use the decorator — that's it
app = typer.Typer()
@app.command()
@pydantic_to_typer(Config)
def run(config: Config):
"""Process images with validation."""
print(config)
if __name__ == "__main__":
app()
$ python example.py --help
Usage: example.py [OPTIONS] IMAGES...
Process images with validation.
╭─ Arguments ──────────────────────────────────────────────────╮
│ * images IMAGES... Image folders to process. [required] │
╰──────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────╮
│ * --output PATH Output directory. [required] │
│ --threshold FLOAT Detection threshold. [default: 0.5]│
│ --seed INTEGER Random seed. │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────╯
How it works
The @pydantic_to_typer(Model) decorator:
- Reads
Model.model_fieldsto discover field names, types, descriptions, and defaults - Strips
Annotatedvalidator metadata to extract the base types Typer understands - Maps
kw_only=False→typer.Argument,kw_only=True→typer.Option - Rewrites the function's
__signature__so Typer sees the expanded parameters - At call time, passes the raw CLI values into
Model(...)so all Pydantic validators run
Your function receives the validated model instance — validators, default_factory, union types, and everything else works exactly as in Pydantic.
Features
| Pydantic | CLI result |
|---|---|
kw_only=False |
typer.Argument (positional) |
kw_only=True or unset |
typer.Option (--flag) |
Field(description=...) |
help=... in the CLI |
Field(default=...) |
Default value shown in --help |
Field(default_factory=...) |
Factory called once at decoration time |
int | None |
Optional CLI option |
list[Path] |
Variadic positional argument |
AfterValidator, BeforeValidator |
Run at call time via Pydantic |
Requirements
- Python ≥ 3.12
- Pydantic ≥ 2.0
- Typer ≥ 0.9
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typantic-0.1.0.tar.gz.
File metadata
- Download URL: typantic-0.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04cf173d55eef67003011a106e1a279118af023c629ba9104b3b89abd59410cf
|
|
| MD5 |
423f9c1ef174090e1ac12be2f586eaa8
|
|
| BLAKE2b-256 |
f4c2cf2601995772311e90c485e9e7f3c0858da05b54cf0473ed267fe19f94f5
|
Provenance
The following attestation bundles were made for typantic-0.1.0.tar.gz:
Publisher:
publish.yml on KiSchnelle/typantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typantic-0.1.0.tar.gz -
Subject digest:
04cf173d55eef67003011a106e1a279118af023c629ba9104b3b89abd59410cf - Sigstore transparency entry: 1573403370
- Sigstore integration time:
-
Permalink:
KiSchnelle/typantic@a0a974bc4014d56e4e65417816695d43fca26b2e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KiSchnelle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0a974bc4014d56e4e65417816695d43fca26b2e -
Trigger Event:
release
-
Statement type:
File details
Details for the file typantic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: typantic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d22e85ecab983f766c512afccbc9afbee9503bf02c99d53a5a7a3b1377e46c1e
|
|
| MD5 |
49020515ab0ab67f5b6ac0143e11af93
|
|
| BLAKE2b-256 |
2e8d5ce37f8dfaf441b600a4c03445ca2a1428deee5feaed48facd83255cd3a9
|
Provenance
The following attestation bundles were made for typantic-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on KiSchnelle/typantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typantic-0.1.0-py3-none-any.whl -
Subject digest:
d22e85ecab983f766c512afccbc9afbee9503bf02c99d53a5a7a3b1377e46c1e - Sigstore transparency entry: 1573403393
- Sigstore integration time:
-
Permalink:
KiSchnelle/typantic@a0a974bc4014d56e4e65417816695d43fca26b2e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KiSchnelle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0a974bc4014d56e4e65417816695d43fca26b2e -
Trigger Event:
release
-
Statement type: