Skip to content

Commit

Permalink
Minor test, readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Sep 6, 2024
1 parent 6ab907f commit cdb0254
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@

<br />

<strong><code>tyro.cli()</code></strong> generates command-line interfaces via
Python type introspection. We can define configurable scripts using functions:
<strong><code>tyro.cli()</code></strong> is a tool for generating CLI
interfaces.

We can define configurable scripts using functions:

```python
"""A command-line interface defined using a function signature.
Expand All @@ -60,7 +62,7 @@ if __name__ == "__main__":
tyro.cli(main)
```

Or instantiate configuration objects defined using tools like `dataclasses`, `pydantic`, and `attrs`:
Or instantiate config objects defined using tools like `dataclasses`, `pydantic`, and `attrs`:

```python
"""A command-line interface defined using a class signature.
Expand All @@ -84,8 +86,8 @@ if __name__ == "__main__":
assert isinstance(config, Config) # Should pass.
```

Other features include helptext generation, nested structures, shell
completion, and subcommands. For examples and the API reference, see our
Other features include helptext generation, nested structures, subcommands, and
shell completion. For examples and the API reference, see our
[documentation](https://brentyi.github.io/tyro).

### In the wild
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dcargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ class A:
assert tyro.cli(A, args=["--x", "3"])


def test_literal_none() -> None:
@dataclasses.dataclass
class A:
x: Literal[0, 1, None, 2]

assert tyro.cli(A, args=["--x", "1"]) == A(x=1)
assert tyro.cli(A, args=["--x", "None"]) == A(x=None)
with pytest.raises(SystemExit):
assert tyro.cli(A, args=["--x", "3"])


Choices = int
Choices = tyro.extras.literal_type_from_choices([0, 1, 2]) # type: ignore

Expand Down
32 changes: 32 additions & 0 deletions tests/test_py311_generated/test_completion_generated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import dataclasses
import io
from typing import Annotated, Literal, Optional

import pytest

Expand Down Expand Up @@ -40,3 +41,34 @@ def test_zsh():
with pytest.raises(SystemExit), contextlib.redirect_stdout(target):
tyro.cli(Wrapper, args=["--tyro-print-completion", "zsh"])
assert "# AUTOMATICALLY GENERATED by `shtab`" in target.getvalue()


def test_completion_zsh():
"""https://github.com/brentyi/tyro/issues/158"""

def start_device(
preset: Annotated[
Optional[Literal["rgb", "depth", "ir"]], tyro.conf.arg(aliases=["-p"])
] = None,
frame: Annotated[
Literal["world", "base"], tyro.conf.arg(aliases=["-f"])
] = "world",
) -> None:
"""
Start device with the given preset.
:param preset: device preset to use.
:param frame: coordinate frame to use.
"""

target = io.StringIO()
with pytest.raises(SystemExit), contextlib.redirect_stdout(target):
tyro.cli(start_device, args=["--tyro-print-completion", "bash"])

completion_script = target.getvalue()
print(completion_script)
assert "# AUTOMATICALLY GENERATED by `shtab`" in completion_script
assert "preset_choices=(" in completion_script
assert "p_choices=(" in completion_script
assert "frame_choices=(" in completion_script
assert "f_choices=(" in completion_script
11 changes: 11 additions & 0 deletions tests/test_py311_generated/test_dcargs_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ class A:
assert tyro.cli(A, args=["--x", "3"])


def test_literal_none() -> None:
@dataclasses.dataclass
class A:
x: Literal[0, 1, None, 2]

assert tyro.cli(A, args=["--x", "1"]) == A(x=1)
assert tyro.cli(A, args=["--x", "None"]) == A(x=None)
with pytest.raises(SystemExit):
assert tyro.cli(A, args=["--x", "3"])


Choices = int
Choices = tyro.extras.literal_type_from_choices([0, 1, 2]) # type: ignore

Expand Down

0 comments on commit cdb0254

Please sign in to comment.