`_.
.. code-block:: python
:linenos:
# 01_generics_py312.py
import dataclasses
import tyro
@dataclasses.dataclass
class Point3[ScalarType: (int, float)]:
x: ScalarType
y: ScalarType
z: ScalarType
frame_id: str
@dataclasses.dataclass
class Triangle:
a: Point3[float]
b: Point3[float]
c: Point3[float]
@dataclasses.dataclass
class Args[ShapeType]:
shape: ShapeType
if __name__ == "__main__":
args = tyro.cli(Args[Triangle])
print(args)
.. raw:: html
$ python ./01_generics_py312.py --help
usage: 01_generics_py312.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) │
╰─────────────────────────────────────────────────────────╯
.. _example-02_generics:
Generics (Legacy)
-----------------
The legacy :py:class:`typing.Generic` and :py:class:`typing.TypeVar` syntax for
generic types is also supported.
.. code-block:: python
:linenos:
# 02_generics.py
import dataclasses
from typing import Generic, TypeVar
import tyro
ScalarType = TypeVar("ScalarType", int, float)
ShapeType = TypeVar("ShapeType")
@dataclasses.dataclass(frozen=True)
class Point3(Generic[ScalarType]):
x: ScalarType
y: ScalarType
z: ScalarType
frame_id: str
@dataclasses.dataclass(frozen=True)
class Triangle:
a: Point3[float]
b: Point3[float]
c: Point3[float]
@dataclasses.dataclass(frozen=True)
class Args(Generic[ShapeType]):
shape: ShapeType
if __name__ == "__main__":
args = tyro.cli(Args[Triangle])
print(args)
.. raw:: html
$ python ./02_generics.py --help
usage: 02_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) │
╰─────────────────────────────────────────────────────────╯