Tuples#

Example using tyro.cli() to instantiate tuple types.

 1from typing import NamedTuple
 2
 3import tyro
 4
 5
 6# Named tuples are interpreted as nested structures.
 7class Color(NamedTuple):
 8    r: int
 9    g: int
10    b: int
11
12
13class TupleType(NamedTuple):
14    """Description.
15    This should show up in the helptext!"""
16
17    # Tuple types can contain raw values.
18    color: tuple[int, int, int] = (255, 0, 0)
19
20    # Tuple types can contain nested structures.
21    two_colors: tuple[Color, Color] = (Color(255, 0, 0), Color(0, 255, 0))
22
23
24if __name__ == "__main__":
25    x = tyro.cli(TupleType)
26    assert isinstance(x, tuple)
27    print(x)

python 04_additional/03_tuples.py --help
usage: 03_tuples.py [-h] [OPTIONS]

Description. This should show up in the helptext!

╭─ options ──────────────────────────────────────────────────────────────────╮
│ -h, --help              show this help message and exit                    │
│ --color INT INT INT     Tuple types can contain raw values. (default: 255  │
│                         0 0)                                               │
╰────────────────────────────────────────────────────────────────────────────╯
╭─ two-colors.0 options ─────────────────────────────────────────────────────╮
│ --two-colors.0.r INT    (default: 255)                                     │
│ --two-colors.0.g INT    (default: 0)                                       │
│ --two-colors.0.b INT    (default: 0)                                       │
╰────────────────────────────────────────────────────────────────────────────╯
╭─ two-colors.1 options ─────────────────────────────────────────────────────╮
│ --two-colors.1.r INT    (default: 0)                                       │
│ --two-colors.1.g INT    (default: 255)                                     │
│ --two-colors.1.b INT    (default: 0)                                       │
╰────────────────────────────────────────────────────────────────────────────╯

python 04_additional/03_tuples.py --color 127 127 127
TupleType(color=(127, 127, 127), two_colors=(Color(r=255, g=0, b=0), Color(r=0, g=255, b=0)))

python 04_additional/03_tuples.py --two-colors.1.r 127 --two-colors.1.g 0 --two-colors.1.b 0
TupleType(color=(255, 0, 0), two_colors=(Color(r=255, g=0, b=0), Color(r=127, g=0, b=0)))