Nesting in Containers#

Structures can be nested inside of standard containers.

Note that lengths must be inferable, either via a fixed-length tuple annotation or by parsing default values.

 1import dataclasses
 2
 3import tyro
 4
 5
 6class Color:
 7    pass
 8
 9
10@dataclasses.dataclass
11class RGB(Color):
12    r: int
13    g: int
14    b: int
15
16
17@dataclasses.dataclass
18class HSL(Color):
19    h: int
20    s: int
21    l: int
22
23
24@dataclasses.dataclass
25class Args:
26    # Example of specifying nested structures via a fixed-length tuple.
27    color_tuple: tuple[RGB, HSL]
28
29    # Examples of nested structures in variable-length containers. These need a default
30    # provided for length inference; we don't currently support specifying dynamic
31    # container lengths directly from the commandline.
32    color_tuple_alt: tuple[Color, ...] = (
33        RGB(255, 0, 0),
34        HSL(0, 255, 0),
35    )
36    color_map: dict[str, RGB] = dataclasses.field(
37        # We can't use mutable values as defaults directly.
38        default_factory={
39            "red": RGB(255, 0, 0),
40            "green": RGB(0, 255, 0),
41            "blue": RGB(0, 0, 255),
42        }.copy
43    )
44
45
46if __name__ == "__main__":
47    args = tyro.cli(Args)
48    print(args)

python 02_nesting/04_nesting_in_containers.py --help
usage: 04_nesting_in_containers.py [-h] [OPTIONS]

╭─ options ───────────────────────────────────────────────╮
│ -h, --help              show this help message and exit │
╰─────────────────────────────────────────────────────────╯
╭─ color-tuple.0 options ─────────────────────────────────╮
│ --color-tuple.0.r INT   (required)                      │
│ --color-tuple.0.g INT   (required)                      │
│ --color-tuple.0.b INT   (required)                      │
╰─────────────────────────────────────────────────────────╯
╭─ color-tuple.1 options ─────────────────────────────────╮
│ --color-tuple.1.h INT   (required)                      │
│ --color-tuple.1.s INT   (required)                      │
│ --color-tuple.1.l INT   (required)                      │
╰─────────────────────────────────────────────────────────╯
╭─ color-tuple-alt.0 options ─────────────────────────────╮
│ --color-tuple-alt.0.r INT                               │
│                         (default: 255)                  │
│ --color-tuple-alt.0.g INT                               │
│                         (default: 0)                    │
│ --color-tuple-alt.0.b INT                               │
│                         (default: 0)                    │
╰─────────────────────────────────────────────────────────╯
╭─ color-tuple-alt.1 options ─────────────────────────────╮
│ --color-tuple-alt.1.h INT                               │
│                         (default: 0)                    │
│ --color-tuple-alt.1.s INT                               │
│                         (default: 255)                  │
│ --color-tuple-alt.1.l INT                               │
│                         (default: 0)                    │
╰─────────────────────────────────────────────────────────╯
╭─ color-map.red options ─────────────────────────────────╮
│ --color-map.red.r INT   (default: 255)                  │
│ --color-map.red.g INT   (default: 0)                    │
│ --color-map.red.b INT   (default: 0)                    │
╰─────────────────────────────────────────────────────────╯
╭─ color-map.green options ───────────────────────────────╮
│ --color-map.green.r INT                                 │
│                         (default: 0)                    │
│ --color-map.green.g INT                                 │
│                         (default: 255)                  │
│ --color-map.green.b INT                                 │
│                         (default: 0)                    │
╰─────────────────────────────────────────────────────────╯
╭─ color-map.blue options ────────────────────────────────╮
│ --color-map.blue.r INT  (default: 0)                    │
│ --color-map.blue.g INT  (default: 0)                    │
│ --color-map.blue.b INT  (default: 255)                  │
╰─────────────────────────────────────────────────────────╯