tyro#

Subpackages#

Package Contents#

tyro.cli(f: tyro._typing.TypeForm[OutT], *, prog: str | None = None, description: str | None = None, args: Sequence[str] | None = None, default: OutT | None = None, return_unknown_args: typing_extensions.Literal[False] = False, use_underscores: bool = False, console_outputs: bool = True) OutT[source]#
tyro.cli(f: tyro._typing.TypeForm[OutT], *, prog: str | None = None, description: str | None = None, args: Sequence[str] | None = None, default: OutT | None = None, return_unknown_args: typing_extensions.Literal[True], use_underscores: bool = False, console_outputs: bool = True) Tuple[OutT, List[str]]
tyro.cli(f: Callable[Ellipsis, OutT], *, prog: str | None = None, description: str | None = None, args: Sequence[str] | None = None, default: None = None, return_unknown_args: typing_extensions.Literal[False] = False, use_underscores: bool = False, console_outputs: bool = True) OutT
tyro.cli(f: Callable[Ellipsis, OutT], *, prog: str | None = None, description: str | None = None, args: Sequence[str] | None = None, default: None = None, return_unknown_args: typing_extensions.Literal[True], use_underscores: bool = False, console_outputs: bool = True) Tuple[OutT, List[str]]

Call or instantiate f, with inputs populated from an automatically generated CLI interface.

f should have type-annotated inputs, and can be a function or type. If f is a type, tyro.cli() returns an instance.

The parser is generated by populating helptext from docstrings and types from annotations; a broad range of core type annotations are supported.

  • Types natively accepted by argparse: str, int, float, pathlib.Path, etc.

  • Default values for optional parameters.

  • Booleans, which are automatically converted to flags when provided a default value.

  • Enums (via enum.Enum).

  • Various annotations from the standard typing library. Some examples:

    • typing.ClassVar[T].

    • typing.Optional[T].

    • typing.Literal[T].

    • typing.Sequence[T].

    • typing.List[T].

    • typing.Dict[K, V].

    • typing.Tuple, such as typing.Tuple[T1, T2, T3] or typing.Tuple[T, ...].

    • typing.Set[T].

    • typing.Final[T] and typing.Annotated[T].

    • typing.Union[T1, T2].

    • Various nested combinations of the above: Optional[Literal[T]], Final[Optional[Sequence[T]]], etc.

  • Hierarchical structures via nested dataclasses, TypedDict, NamedTuple, classes.

    • Simple nesting.

    • Unions over nested structures (subparsers).

    • Optional unions over nested structures (optional subparsers).

  • Generics (including nested generics).

Completion script generation for interactive shells is also provided. To write a script that can be used for tab completion, pass in:

--tyro-write-completion {bash/zsh/tcsh} {path to script to write}.

Parameters:
  • f – Function or type.

  • prog – The name of the program printed in helptext. Mirrors argument from argparse.ArgumentParser().

  • description – Description text for the parser, displayed when the –help flag is passed in. If not specified, f‘s docstring is used. Mirrors argument from argparse.ArgumentParser().

  • args – If set, parse arguments from a sequence of strings instead of the commandline. Mirrors argument from argparse.ArgumentParser.parse_args().

  • default – An instance of OutT to use for default values; supported if f is a type like a dataclass or dictionary, but not if f is a general callable like a function or standard class. Helpful for merging CLI arguments with values loaded from elsewhere. (for example, a config object loaded from a yaml file)

  • return_unknown_args – If True, return a tuple of the output of f and a list of unknown arguments. Mirrors the unknown arguments returned from argparse.ArgumentParser.parse_known_args().

  • use_underscores – If True, use underscores as a word delimeter instead of hyphens. This primarily impacts helptext; underscores and hyphens are treated equivalently when parsing happens. We default helptext to hyphens to follow the GNU style guide. https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html

  • console_outputs – If set to False, parsing errors and help messages will be supressed. This can be useful for distributed settings, where tyro.cli() is called from multiple workers but we only want console outputs from the main one.

Returns:

The output of f(...) or an instance f. If f is a class, the two are equivalent. If return_unknown_args is True, returns a tuple of the output of f(...) and a list of unknown arguments.

tyro.MISSING: Any#

Sentinel value to mark fields as missing. Can be used to mark fields passed in as a default_instance for tyro.cli() as required.

exception tyro.UnsupportedTypeAnnotationError[source]#

Bases: Exception

Exception raised when an unsupported type annotation is detected.

tyro.__version__ = '0.8.4'#