Skip to main content

Extensions for the python typer library

Project description

Typer Model Args

An extension for Typer that enables model arguments.

Typer Model Args lets you declare Pydantic model parameters in your Typer command and automatically exposes its fields as proper Typer options/arguments on the command line. It keeps Typer’s annotations, adds missing ones when possible, and can convert typing.Literal fields into Enums for better CLI help and validation.

  • Works with Typer
  • Works with Pydantic models
  • Preserves Typer metadata/annotations
  • Optional Literal-to-Enum conversion for cleaner help and choices

Installation

uv add typer_model_args

Quickstart

# app.py
import typer
from typing import Annotated
from pydantic import BaseModel
from typer_model_args import flatten_parameter_model_to_signature

class CliArgs(BaseModel):
    name: Annotated[str, typer.Option(...)]
    description: str | None = None

app = typer.Typer()

@app.command()
@flatten_parameter_model_to_signature()
def create(arguments: CliArgs, another_arg: str):
    """
    After decoration, --name/--description become top-level CLI options.
    """
    typer.echo(f"name={arguments.name}, desc={arguments.description}, extra={another_arg}")

if __name__ == "__main__":
    app()

API

  • flatten_parameter_model_to_signature(*, literals_to_enums: bool = True)
    • Flattens all pydantic model parameters in the decorated function into the function signature used by Typer.
    • literals_to_enums: when True, fields annotated with typing.Literal are turned into Enums so Typer can display and validate choices.

Why use this?

  • Keep your CLI schema and validation centralized in Pydantic models.
  • Get clean, well-typed Typer commands without manually duplicating model fields as options.
  • Better help messages and validation with minimal boilerplate.

Requirements

  • Python 3.10+
  • Typer
  • Pydantic

License

MIT

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_model_args-0.3.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

typer_model_args-0.3.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file typer_model_args-0.3.1.tar.gz.

File metadata

  • Download URL: typer_model_args-0.3.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for typer_model_args-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a81c1dd0c1174d64ecc7d59f72327e892d08725d0a63ca23c40da79565761a78
MD5 b2bca676c3cb25a3d9a9cc4ba53c0d26
BLAKE2b-256 34f9e03e071148f4c9f510bd162657f9586b1c9ca9688ea53b646c39ef015df3

See more details on using hashes here.

File details

Details for the file typer_model_args-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typer_model_args-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c301533e854fd9eee57d0e8e1280de7ca0c4961d34eaafcc56bae747e8e48447
MD5 64c780979ea1c06a4e21053c064f3853
BLAKE2b-256 729b9f1cee42f29a657ee10f22a0e57504c2abd8be4a5fd15eddfdd65c07ecd0

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