tyro.constructors._primitive_spec

Module Contents

exception tyro.constructors._primitive_spec.UnsupportedTypeAnnotationError(message: tuple[tyro._fmtlib._Text, Ellipsis])[source]

Bases: Exception

Exception raised when a type annotation is not supported.

Parameters:

message (tuple[tyro._fmtlib._Text, Ellipsis])

tyro.constructors._primitive_spec.T
class tyro.constructors._primitive_spec.PrimitiveTypeInfo[source]

Information used to generate constructors for primitive types.

type: Type

Annotated field type. Forward references, aliases, and type variables/parameters will have been resolved and runtime annotations (typing.Annotated) will have been stripped.

type_origin: Type | None

The output of get_origin() on the static type.

markers: set[tyro.conf._markers.Marker]

Set of tyro markers used to configure this field.

static make(raw_annotation: Type | Callable, parent_markers: set[tyro.conf._markers.Marker], exclude_markers: set[tyro.conf._markers.Marker] | None = None) → PrimitiveTypeInfo[source]
Parameters:
  • raw_annotation (Type | Callable)

  • parent_markers (set[tyro.conf._markers.Marker])

  • exclude_markers (set[tyro.conf._markers.Marker] | None)

Return type:

PrimitiveTypeInfo

class tyro.constructors._primitive_spec.PrimitiveConstructorSpec[source]

Bases: Generic[T]

Specification for constructing a primitive type from a string.

There are two ways to use this class:

First, we can include it in a type signature via typing.Annotated. This is the simplest for making local modifications to parsing behavior for individual fields.

Alternatively, it can be returned by a rule in a ConstructorRegistry.

nargs: int | tuple[int, Ellipsis] | Literal[*]

Number of arguments required to construct an instance. If nargs is “*”, then the number of arguments is variable. If nargs is a tuple, it represents multiple valid fixed argument counts (used for unions with different fixed nargs).

metavar: str

Metavar to display in help messages.

instance_from_str: Callable[[list[str]], T]

Given a list of string arguments, construct an instance of the type. The length of the list will match the value of nargs.

is_instance: Callable[[Any], bool | Literal[~]]

Given an object instance, does it match this primitive type? This is used for specific help messages when both a union type is present and a default is provided.

Can return “~” to signify that an instance is a “fuzzy” match, and should only be used if there are no other matches. This is used for numeric tower support.

str_from_instance: Callable[[T], list[str]]

Convert an instance to a list of string arguments that would construct the instance. This is used for help messages when a default is provided.

choices: tuple[str, Ellipsis] | None

Finite set of choices for arguments.

tyro.constructors._primitive_spec.apply_default_primitive_rules(registry: tyro.constructors._registry.ConstructorRegistry) → None[source]

Apply default rules to the registry.

Parameters:

registry (tyro.constructors._registry.ConstructorRegistry)

Return type:

None