diff --git a/docs/source/examples/custom_constructors.rst b/docs/source/examples/custom_constructors.rst index cd9eb459..38f8ce63 100644 --- a/docs/source/examples/custom_constructors.rst +++ b/docs/source/examples/custom_constructors.rst @@ -301,6 +301,9 @@ add support for a custom type. def __init__(self, lower: int, upper: int): self.bounds = (lower, upper) + def __repr__(self) -> str: + return f"(lower={self.bounds[0]}, upper={self.bounds[1]})" + # Create a custom registry, which stores constructor rules. custom_registry = tyro.constructors.ConstructorRegistry() @@ -368,26 +371,24 @@ add support for a custom type. .. raw:: html
- $ python ./03_primitive_registry.py --help - usage: 03_primitive_registry.py [-h] --dict1 JSON [--dict2 JSON] + $ python ./04_struct_registry.py --help + usage: 04_struct_registry.py [-h] [OPTIONS] - A function with two arguments, which can be populated from the CLI via JSON. + A function with two `Bounds` instances as input. - ╭─ options ───────────────────────────────────────────╮ - │ -h, --help show this help message and exit │ - │ --dict1 JSON (required) │ - │ --dict2 JSON (default: '{"default": null}') │ - ╰─────────────────────────────────────────────────────╯ -- - - -.. raw:: html - -
- $ python ./03_primitive_registry.py --dict1 '{"hello": "world"}' - dict1={'hello': 'world'} - dict2={'default': None} + ╭─ options ───────────────────────────────────────────────╮ + │ -h, --help show this help message and exit │ + ╰─────────────────────────────────────────────────────────╯ + ╭─ bounds options ────────────────────────────────────────╮ + │ --bounds.lower INT Lower bound. (required) │ + │ --bounds.upper INT Upper bound. (required) │ + ╰─────────────────────────────────────────────────────────╯ + ╭─ bounds-with-default options ───────────────────────────╮ + │ --bounds-with-default.lower INT │ + │ Lower bound. (default: 0) │ + │ --bounds-with-default.upper INT │ + │ Upper bound. (default: 100) │ + ╰─────────────────────────────────────────────────────────╯@@ -395,7 +396,7 @@ add support for a custom type. .. raw:: html
- $ python ./03_primitive_registry.py --dict1 '{"hello": "world"}' --dict2 '{"hello": "world"}' - dict1={'hello': 'world'} - dict2={'hello': 'world'} + $ python ./04_struct_registry.py --bounds.lower 5 --bounds.upper 10 + bounds=(lower=5, upper=10) + bounds_with_default=(lower=0, upper=100)\ No newline at end of file diff --git a/examples/06_custom_constructors/04_struct_registry.py b/examples/06_custom_constructors/04_struct_registry.py index e836a627..da09e992 100644 --- a/examples/06_custom_constructors/04_struct_registry.py +++ b/examples/06_custom_constructors/04_struct_registry.py @@ -8,10 +8,8 @@ This will be complicated! Usage: - - python ./03_primitive_registry.py --help - python ./03_primitive_registry.py --dict1 '{"hello": "world"}' - python ./03_primitive_registry.py --dict1 '{"hello": "world"}' --dict2 '{"hello": "world"}' + python ./04_struct_registry.py --help + python ./04_struct_registry.py --bounds.lower 5 --bounds.upper 10 """ import tyro @@ -22,6 +20,9 @@ class Bounds: def __init__(self, lower: int, upper: int): self.bounds = (lower, upper) + def __repr__(self) -> str: + return f"(lower={self.bounds[0]}, upper={self.bounds[1]})" + # Create a custom registry, which stores constructor rules. custom_registry = tyro.constructors.ConstructorRegistry()