Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs, custom constructor refinements #194

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def docstring(app, what, name, obj, options, lines):
rst = m2r2.convert(md)
lines.clear()
lines += rst.splitlines() # type: ignore
lines.append("")
lines.append("")


def setup(app):
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/04_additional/06_generics_py312.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. Comment: this file is automatically generated by `update_example_docs.py`.
It should not be modified manually.
Generic Types (Python 3.12+ syntax)
Generic Types (Python 3.12+)
==========================================

Example of parsing for generic dataclasses using syntax introduced in Python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,54 +43,54 @@ Argument Aliases

.. raw:: html

<kbd>python 04_additional/17_aliases.py --help</kbd>
<kbd>python 04_additional/11_aliases.py --help</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py --help
.. program-output:: python ../../examples/04_additional/11_aliases.py --help

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py commit --help</kbd>
<kbd>python 04_additional/11_aliases.py commit --help</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py commit --help
.. program-output:: python ../../examples/04_additional/11_aliases.py commit --help

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py commit --message hello --all</kbd>
<kbd>python 04_additional/11_aliases.py commit --message hello --all</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py commit --message hello --all
.. program-output:: python ../../examples/04_additional/11_aliases.py commit --message hello --all

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py commit -m hello -a</kbd>
<kbd>python 04_additional/11_aliases.py commit -m hello -a</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py commit -m hello -a
.. program-output:: python ../../examples/04_additional/11_aliases.py commit -m hello -a

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py checkout --help</kbd>
<kbd>python 04_additional/11_aliases.py checkout --help</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py checkout --help
.. program-output:: python ../../examples/04_additional/11_aliases.py checkout --help

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py checkout --branch main</kbd>
<kbd>python 04_additional/11_aliases.py checkout --branch main</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py checkout --branch main
.. program-output:: python ../../examples/04_additional/11_aliases.py checkout --branch main

------------

.. raw:: html

<kbd>python 04_additional/17_aliases.py checkout -b main</kbd>
<kbd>python 04_additional/11_aliases.py checkout -b main</kbd>

.. program-output:: python ../../examples/04_additional/17_aliases.py checkout -b main
.. program-output:: python ../../examples/04_additional/11_aliases.py checkout -b main
84 changes: 0 additions & 84 deletions docs/source/examples/04_additional/11_custom_constructors.rst

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ In Python 3.12, the :code:`type` statement is introduced to create type aliases.

.. raw:: html

<kbd>python 04_additional/16_type_statement.py --help</kbd>
<kbd>python 04_additional/12_type_statement.py --help</kbd>

.. program-output:: python ../../examples/04_additional/16_type_statement.py --help
.. program-output:: python ../../examples/04_additional/12_type_statement.py --help
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.. 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"}'</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"}'

------------

.. 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"}'
Loading
Loading