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.2.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.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typer_model_args-0.3.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2a74bc35b199e51ec981dfbda129cf03741d080037eaa9f9fb2661cf743cb3e7
MD5 42fa7b764cd2b2e0cc89237335374007
BLAKE2b-256 fdb260a1da4bd3c33cdcfd9e6f698732bc7f8078e35862ad67b50d66f361ed4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typer_model_args-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a65fde989dd2b797d4805f8c60d0c42c3b4d7b05a44715b7d4e454457ae80953
MD5 ea30b3476bbe282d3c697748672a1463
BLAKE2b-256 e903721e0a06931797a1d5724eb60f89fd737cf58442095379fb21b3e2cf208c

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