Skip to main content

Typer Command and Parameter Domain Model

The data model of Typer revolves around the Typer application class and a set of "Info" classes that store metadata about commands, sub-apps, and parameters.

Key Components:

  • Typer (App): The main entry point. It maintains lists of registered commands (CommandInfo) and sub-apps (TyperInfo). It also holds its own metadata in a TyperInfo object.
  • TyperInfo: Metadata for a Typer application or a sub-application (group). When a sub-app is added via add_typer(), it is wrapped in a TyperInfo object which references the sub-app's Typer instance.
  • CommandInfo: Metadata for a single CLI command. It stores the callback function and help text.
  • ParamMeta: A bridge entity created during function signature inspection. It links a function parameter to its Typer-specific metadata (ParameterInfo).
  • ParameterInfo: The base class for parameter metadata, containing common fields like default, help, and callback.
  • ArgumentInfo & OptionInfo: Specialized metadata for positional arguments and named options, respectively. They inherit from ParameterInfo.

Relationships:

  • A Typer app has a main TyperInfo and can have multiple CommandInfo (commands) and TyperInfo (sub-apps) objects.
  • Both TyperInfo and CommandInfo reference a Callable (the user-defined function).
  • Typer inspects these Callable objects to produce ParamMeta for each parameter.
  • Each ParamMeta may reference a ParameterInfo (either an ArgumentInfo or OptionInfo) which was provided as a default value or via Annotated.
  • TyperInfo for a sub-app points back to the Typer instance it represents, allowing for a recursive tree of applications.

Key Architectural Findings:

  • Typer uses a set of 'Info' classes (TyperInfo, CommandInfo, ParameterInfo) to store metadata before converting them to Click objects.
  • The Typer class acts as a container for registered commands and sub-apps, which are stored as CommandInfo and TyperInfo respectively.
  • ParamMeta is an internal model used during function inspection to map Python parameters to Typer's ArgumentInfo or OptionInfo.
  • TyperInfo is used both for the main app's metadata and for representing sub-apps (groups) in the command hierarchy.
  • Inheritance is used for parameters: ArgumentInfo and OptionInfo both derive from ParameterInfo.
Loading diagram...