Attrs Integration#
In addition to standard dataclasses, tyro
also supports
attrs classes.
1import attr
2
3import tyro
4
5
6@attr.s
7class Args:
8 """Description.
9 This should show up in the helptext!"""
10
11 field1: str = attr.ib()
12 """A string field."""
13
14 field2: int = attr.ib(factory=lambda: 5)
15 """A required integer field."""
16
17
18if __name__ == "__main__":
19 args = tyro.cli(Args)
20 print(args)
python 04_additional/09_attrs.py --help
usage: 09_attrs.py [-h] --field1 STR [--field2 INT] Description. This should show up in the helptext! ╭─ options ──────────────────────────────────────────────────╮ │ -h, --help show this help message and exit │ │ --field1 STR A string field. (required) │ │ --field2 INT A required integer field. (default: 5) │ ╰────────────────────────────────────────────────────────────╯
python 04_additional/09_attrs.py --field1 hello
Args(field1='hello', field2=5)
python 04_additional/09_attrs.py --field1 hello --field2 5
Args(field1='hello', field2=5)