-
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.
- Loading branch information
Showing
45 changed files
with
568 additions
and
235 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Enums | ||
========================================== | ||
|
||
|
||
We can generate argument parsers from more advanced type annotations, like enums. | ||
|
||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import dataclasses | ||
import enum | ||
import pathlib | ||
from typing import Optional, Tuple | ||
import tyro | ||
class OptimizerType(enum.Enum): | ||
ADAM = enum.auto() | ||
SGD = enum.auto() | ||
@dataclasses.dataclass(frozen=True) | ||
class TrainConfig: | ||
# Enums are handled seamlessly. | ||
optimizer_type: OptimizerType = OptimizerType.ADAM | ||
"""Gradient-based optimizer to use.""" | ||
learning_rate: float = 1e-4 | ||
"""Learning rate for optimizer.""" | ||
if __name__ == "__main__": | ||
config = tyro.cli(TrainConfig) | ||
print(config) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 01_basics/04_enums.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/01_basics/04_enums.py --help | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 01_basics/04_enums.py --optimizer-type SGD</kbd> | ||
|
||
.. program-output:: python ../../examples/01_basics/04_enums.py --optimizer-type SGD | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 01_basics/04_enums.py --optimizer-type ADAM --learning-rate 3e-4</kbd> | ||
|
||
.. program-output:: python ../../examples/01_basics/04_enums.py --optimizer-type ADAM --learning-rate 3e-4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Choices | ||
========================================== | ||
|
||
|
||
``typing.Literal[]`` can be used to restrict inputs to a fixed set of literal choices. | ||
|
||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import dataclasses | ||
import enum | ||
from typing import Literal, Optional, Tuple, Union | ||
import tyro | ||
class Color(enum.Enum): | ||
RED = enum.auto() | ||
GREEN = enum.auto() | ||
BLUE = enum.auto() | ||
@dataclasses.dataclass(frozen=True) | ||
class Args: | ||
# We can use Literal[] to restrict the set of allowable inputs, for example, over | ||
# enums. | ||
restricted_enum: Literal[Color.RED, Color.GREEN] = Color.RED | ||
# Or mix them with other types! | ||
mixed: Literal[Color.RED, Color.GREEN, "blue"] = "blue" | ||
if __name__ == "__main__": | ||
args = tyro.cli(Args) | ||
print(args) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 01_basics/06_literals.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/01_basics/06_literals.py --help |
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
Oops, something went wrong.