API Reference

Typer

Library for building CLIs in Python from type annotations, built on Click with automatic help and completion.

Official Documentation ↗
CompatibilityThis documentation covers version v0.12. Check the official docs for the latest API surface.

Installation

pippip install typer

Methods

Library for building CLIs in Python from type annotations, built on Click with automatic help and completion.

Overview

Typer turns function signatures into command-line interfaces. Parameter names, type annotations, and defaults become arguments, options, and validation; docstrings become help text; and shell completion is generated for common shells.

Built on Click, it stays compatible with the wider ecosystem while removing boilerplate. Subcommands compose through a Typer app, Enum types constrain choices, and files or paths arrive as typed values. It is the CLI layer of choice in the FastAPI tooling family.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • typer.Typer — Creates the CLI application; decorated functions become commands and app() runs the parser.
  • typer.Option / typer.Argument — Annotates parameters as CLI arguments or options with defaults, help text, and flag aliases.
  • typer.run — Single-command shortcut that turns one function directly into a CLI with no app object.
  • typer.echo / typer.Exit — Prints output, optionally to stderr, and exits with a controlled status code for CI signals.

Usage Notes

  • Type parameters precisely: int, float, Enum, pathlib.Path each convert and validate input for free.
  • Reserve typer.Exit for terminal outcomes; raise domain exceptions and translate them at the app boundary instead.
  • Keep commands as thin adapters that parse input and delegate to importable functions, which keeps logic testable.

Common Pitfalls

  • Mutable defaults and Optional types translate poorly to CLI options; prefer explicit defaults.
  • Help text is generated from docstrings; stale docstrings ship stale help.
  • typer.run supports one command only, multi-command apps need a Typer instance with app.command.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://typer.tiangolo.com/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:10.981Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Dependencies ¶ Typer requires only a few dependencies (most are tiny): rich : to show nicely formatted errors automatically. (https://typer.tiangolo.com/)

FAQ

How does Typer relate to Click?
Typer is a thin layer over Click: a Typer app converts to a Click command under the hood. You get Click maturity plus type-annotation ergonomics, and you can drop to native Click decorators when needed.
How are invalid values reported?
Type annotations drive validation before your function runs: int parameters reject non-numeric input, Enum parameters enforce choices, and failures produce a usage error with exit code 2 automatically.
Does Typer support async commands?
Async functions are supported through AnyIO; commands declared async are awaited automatically. For long-running async work, keep the command thin and drive the loop from your own runner for explicit lifecycle control.