Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsApr 14, 2026·3 min de lectura

Typer — Build Python CLIs from Type Hints

Typer turns Python functions into CLI commands using only type hints. It is built on Click but removes the boilerplate — write a function, and Typer auto-generates parsing, help text, and shell completions.

Listo para agents

Staging seguro para este activo

Este activo primero queda en staging. El prompt copiado pide inspeccionar los archivos staged antes de activar scripts, config MCP o config global.

Stage only · 17/100Política: staging
Superficie agent
Cualquier agent MCP/CLI
Tipo
CLI Tool
Instalación
Stage only
Confianza
Confianza: Established
Entrada
step-1.md
Comando de staging seguro
npx -y tokrepo@latest install 6942f03e-37b5-11f1-9bc6-00163e2b0d79 --target codex

Primero deja archivos en staging; la activación requiere revisar el README y el plan staged.

TL;DR
Typer converts Python functions with type hints into full CLI commands with auto-generated parsing, help text, and completions.
§01

What it is

Typer is a Python library that turns regular Python functions into command-line interface commands using only type hints. Built on top of Click, Typer removes the boilerplate of argument parsing, help text generation, and shell completion setup.

Typer is created by the same developer behind FastAPI. It follows the same philosophy: use standard Python type annotations to define the interface, and let the framework handle the rest.

§02

How it saves time or tokens

Traditional CLI libraries like argparse require verbose argument definitions separate from the function logic. Typer eliminates this duplication. The function signature is the CLI definition. A single function with type-annotated parameters becomes a complete CLI command with help text, validation, and tab completion.

For AI agents generating CLI tools, Typer is the shortest path from a Python function to a working command. The type-hint-driven approach means LLMs can produce correct CLI code with minimal context.

§03

How to use

  1. Install Typer:
pip install typer
  1. Write a function with type hints:
import typer

def greet(name: str, count: int = 1, formal: bool = False):
    greeting = 'Good day' if formal else 'Hello'
    for _ in range(count):
        print(f'{greeting}, {name}')

if __name__ == '__main__':
    typer.run(greet)
  1. Run it from the terminal:
python greet.py --name World --count 3 --formal

Typer auto-generates --help, validates types, and provides shell completion.

§04

Example

import typer

app = typer.Typer()

@app.command()
def deploy(env: str = typer.Argument(..., help='Target environment'),
           dry_run: bool = typer.Option(False, help='Preview without applying')):
    if dry_run:
        print(f'Would deploy to {env}')
    else:
        print(f'Deploying to {env}...')

@app.command()
def rollback(env: str, steps: int = 1):
    print(f'Rolling back {steps} step(s) in {env}')

if __name__ == '__main__':
    app()

This creates a CLI with deploy and rollback subcommands, each with typed arguments and auto-generated help.

§05

Related on TokRepo

§06

Common pitfalls

  • Forgetting that Typer requires Python 3.7+ for proper type hint support. Older Python versions will not work correctly with the annotation-driven approach.
  • Not installing the shellingham and click-completion extras for shell completion. Run pip install 'typer[all]' to get the full feature set.
  • Using mutable default arguments (like lists or dicts) as parameter defaults. Use typer.Option(default_factory=list) instead to avoid shared state bugs.
  • Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.

Preguntas frecuentes

How does Typer compare to Click?+

Typer is built on top of Click and uses it under the hood. The difference is the developer experience: Click requires decorators and explicit parameter definitions, while Typer infers everything from function signatures and type hints. Typer produces the same result with less code.

Can Typer create multi-command CLI applications?+

Yes. Use the typer.Typer() app object and decorate functions with @app.command(). Each decorated function becomes a subcommand. You can also nest Typer apps for complex CLI hierarchies with command groups.

Does Typer support shell completion?+

Yes. Typer generates shell completion scripts for bash, zsh, fish, and PowerShell. Install with typer[all] and run the completion install command. Arguments, options, and even Enum values get tab-completed automatically.

Who created Typer?+

Typer was created by Sebastian Ramirez, the same developer behind FastAPI. Both libraries share the philosophy of using Python type annotations to define interfaces and auto-generate documentation and validation.

Can AI agents use Typer to build CLI tools?+

Yes. Typer's type-hint-driven approach is well-suited for AI code generation. An LLM can produce a complete CLI tool by writing a single Python function with type annotations. The minimal boilerplate means fewer opportunities for generation errors.

Referencias (3)

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados