tyro.constructors._registry

Module Contents

tyro.constructors._registry.current_registry: ConstructorRegistry | None
tyro.constructors._registry.PrimitiveSpecRule
tyro.constructors._registry.StructSpecRule
tyro.constructors._registry.check_default_instances() bool[source]

Check whether we should be strict about checking that default types and instances match.

This is usually False; tyro attempts to be somewhat lenient when inconsistent types are encounted. Strictness, however, is useful for matching annotated subcommands to default values.

Return type:

bool

tyro.constructors._registry.check_default_instances_context()[source]

Context for whether we should be strict about checking that default types and instances match.

This is usually False; tyro attempts to be somewhat lenient when inconsistent types are encounted. Strictness, however, is useful for matching annotated subcommands to default values.

class tyro.constructors._registry.ConstructorRegistry[source]

Registry for rules that define how types are constructed from command-line arguments.

The behavior of CLIs generated by tyro are based on two types of rules.

Primitive rules should be a callable with the signature: `python (type_info: PrimitiveTypeInfo) -> PrimitiveConstructorSpec | None ` where None is returned if the rule doesn’t apply. Each primitive rule defines behavior for a type that can be instantiated from a single command-line argument.

Struct rules should be a callable with the signature: `python (type_info: StructTypeInfo) -> StructConstructorSpec | None ` where None is returned if the rule doesn’t apply. Each struct rule defines behavior for a type that can be instantiated from multiple command-line arguments.

To activate a registry, use it as a context manager. For example:

```python registry = ConstructorRegistry()

with registry:

tyro.cli(…)

```

primitive_rule(rule: PrimitiveSpecRule) PrimitiveSpecRule[source]

Define a rule for constructing a primitive type from a string. The most recently added rule will be applied first.

Custom primitive rules will take precedence over both default primitive rules and struct rules

Parameters:

rule (PrimitiveSpecRule)

Return type:

PrimitiveSpecRule

struct_rule(rule: StructSpecRule) StructSpecRule[source]

Define a rule for constructing a primitive type from a string. The most recently added rule will be applied first.

Parameters:

rule (StructSpecRule)

Return type:

StructSpecRule

get_primitive_spec(type_info: tyro.constructors._primitive_spec.PrimitiveTypeInfo, rule_mode: typing_extensions.Literal[default, custom, all] = 'all') tyro.constructors._primitive_spec.PrimitiveConstructorSpec[source]

Get a constructor specification for a given type.

Parameters:
Return type:

tyro.constructors._primitive_spec.PrimitiveConstructorSpec

get_struct_spec(type_info: tyro.constructors._struct_spec.StructTypeInfo) tyro.constructors._struct_spec.StructConstructorSpec | None[source]

Get a constructor specification for a given type. Returns None if unsuccessful.

Parameters:

type_info (tyro.constructors._struct_spec.StructTypeInfo)

Return type:

tyro.constructors._struct_spec.StructConstructorSpec | None

__enter__() None[source]
Return type:

None

__exit__(*args: Any) None[source]
Parameters:

args (Any)

Return type:

None