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

Uploaded Python 3

File details

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

File metadata

  • Download URL: typer_model_args-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 1b9fd3b29eb873d3dca11928d3b5c67d6d9ad2fee070f1508ce9233706fadb1d
MD5 10257b96a7b2ba0b11cb914671ae9d33
BLAKE2b-256 21c8217347ce67a4ad82417c414382d38b2392ac6d71d459907d940804763fbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typer_model_args-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 964ed8dd50856604a2ef6b7033f5d593b1e89deef0e2c10d4dd900b54a5ce407
MD5 13c9d915611c6e1ce9a8b54c37cdc5ea
BLAKE2b-256 1c43a7dfa63253ab987cdcdf3994909c59d24d38329b3fa2cec659fe5443e2b7

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