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 aTyperInfoobject. - TyperInfo: Metadata for a Typer application or a sub-application (group). When a sub-app is added via
add_typer(), it is wrapped in aTyperInfoobject which references the sub-app'sTyperinstance. - 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, andcallback. - ArgumentInfo & OptionInfo: Specialized metadata for positional arguments and named options, respectively. They inherit from
ParameterInfo.
Relationships:
- A
Typerapp has a mainTyperInfoand can have multipleCommandInfo(commands) andTyperInfo(sub-apps) objects. - Both
TyperInfoandCommandInforeference aCallable(the user-defined function). - Typer inspects these
Callableobjects to produceParamMetafor each parameter. - Each
ParamMetamay reference aParameterInfo(either anArgumentInfoorOptionInfo) which was provided as a default value or viaAnnotated. TyperInfofor a sub-app points back to theTyperinstance 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...