Type Aliases (Python 3.12+)#

In Python 3.12, the type statement is introduced to create type aliases.

 1import dataclasses
 2
 3import tyro
 4
 5# Lazily-evaluated type alias.
 6type Field1Type = Inner
 7
 8
 9@dataclasses.dataclass
10class Inner:
11    a: int
12    b: str
13
14
15@dataclasses.dataclass
16class Args:
17    """Description.
18    This should show up in the helptext!"""
19
20    field1: Field1Type
21    """A field."""
22
23    field2: int = 3
24    """A numeric field, with a default value."""
25
26
27if __name__ == "__main__":
28    args = tyro.cli(Args)
29    print(args)

python 04_additional/13_type_statement.py --help
usage: 13_type_statement.py [-h] [--field2 INT] --field1.a INT --field1.b STR

Description. This should show up in the helptext!

╭─ options ─────────────────────────────────────────────────────────────────╮
│ -h, --help            show this help message and exit                     │
│ --field2 INT          A numeric field, with a default value. (default: 3) │
╰───────────────────────────────────────────────────────────────────────────╯
╭─ field1 options ──────────────────────────────────────────────────────────╮
│ A field.                                                                  │
│ ────────────────────────────────                                          │
│ --field1.a INT        (required)                                          │
│ --field1.b STR        (required)                                          │
╰───────────────────────────────────────────────────────────────────────────╯