Generic Types#
Example of parsing for generic dataclasses.
1import dataclasses
2from typing import Generic, TypeVar
3
4import tyro
5
6ScalarType = TypeVar("ScalarType", int, float)
7ShapeType = TypeVar("ShapeType")
8
9
10@dataclasses.dataclass(frozen=True)
11class Point3(Generic[ScalarType]):
12 x: ScalarType
13 y: ScalarType
14 z: ScalarType
15 frame_id: str
16
17
18@dataclasses.dataclass(frozen=True)
19class Triangle:
20 a: Point3[float]
21 b: Point3[float]
22 c: Point3[float]
23
24
25@dataclasses.dataclass(frozen=True)
26class Args(Generic[ShapeType]):
27 shape: ShapeType
28
29
30if __name__ == "__main__":
31 args = tyro.cli(Args[Triangle])
32 print(args)
python 04_additional/05_generics.py --help
usage: 05_generics.py [-h] [OPTIONS] ╭─ options ───────────────────────────────────────────────╮ │ -h, --help show this help message and exit │ ╰─────────────────────────────────────────────────────────╯ ╭─ shape.a options ───────────────────────────────────────╮ │ --shape.a.x FLOAT (required) │ │ --shape.a.y FLOAT (required) │ │ --shape.a.z FLOAT (required) │ │ --shape.a.frame-id STR (required) │ ╰─────────────────────────────────────────────────────────╯ ╭─ shape.b options ───────────────────────────────────────╮ │ --shape.b.x FLOAT (required) │ │ --shape.b.y FLOAT (required) │ │ --shape.b.z FLOAT (required) │ │ --shape.b.frame-id STR (required) │ ╰─────────────────────────────────────────────────────────╯ ╭─ shape.c options ───────────────────────────────────────╮ │ --shape.c.x FLOAT (required) │ │ --shape.c.y FLOAT (required) │ │ --shape.c.z FLOAT (required) │ │ --shape.c.frame-id STR (required) │ ╰─────────────────────────────────────────────────────────╯