-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs, custom constructor refinements (#194)
* Docs pass, primitive rule priority * Fix default primitive rules * tests * sync docs * union fix
- Loading branch information
Showing
37 changed files
with
539 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 0 additions & 84 deletions
84
docs/source/examples/04_additional/11_custom_constructors.rst
This file was deleted.
Oops, something went wrong.
92 changes: 0 additions & 92 deletions
92
docs/source/examples/04_additional/12_custom_constructors_registry.rst
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
docs/source/examples/05_custom_constructors/01_primitive_annotation.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Custom Primitive | ||
========================================== | ||
|
||
For additional flexibility, :mod:`tyro.constructors` exposes tyro's API for | ||
defining behavior for different types. There are two categories of types: | ||
primitive types can be instantiated from a single commandline argument, while | ||
struct types are broken down into multiple. | ||
|
||
In this example, we attach a custom constructor via a runtime annotation. | ||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import json | ||
from typing_extensions import Annotated | ||
import tyro | ||
# A dictionary type, but `tyro` will expect a JSON string from the CLI. | ||
JsonDict = Annotated[ | ||
dict, | ||
tyro.constructors.PrimitiveConstructorSpec( | ||
nargs=1, | ||
metavar="JSON", | ||
instance_from_str=lambda args: json.loads(args[0]), | ||
is_instance=lambda instance: isinstance(instance, dict), | ||
str_from_instance=lambda instance: [json.dumps(instance)], | ||
), | ||
] | ||
def main( | ||
dict1: JsonDict, | ||
dict2: JsonDict = {"default": None}, | ||
) -> None: | ||
print(f"{dict1=}") | ||
print(f"{dict2=}") | ||
if __name__ == "__main__": | ||
tyro.cli(main) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 05_custom_constructors/01_primitive_annotation.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/05_custom_constructors/01_primitive_annotation.py --help | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 05_custom_constructors/01_primitive_annotation.py --dict1 '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/05_custom_constructors/01_primitive_annotation.py --dict1 '{"hello": "world"}' | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 05_custom_constructors/01_primitive_annotation.py --dict1 '{"hello": "world"}' --dict2 '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/05_custom_constructors/01_primitive_annotation.py --dict1 '{"hello": "world"}' --dict2 '{"hello": "world"}' |
Oops, something went wrong.