New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting docstrings for dynamic dataclasses #25
Comments
In case this is a feature that you'd be interested in supporting, I put up a PR here: #26 . I'm able to get dynamic dataclasses with automatically parsed docstring annotations working with this change :D |
Thanks @tovacinni! This is a good question, probably also relevant to #23. There should be (at least?) two existing approaches for getting helptext messages here. Curious about your thoughts on how they compare to your solution:
(1) is the API I would encourage; it's verbose, but more general / not restricted to dataclasses and avoids the string dictionary key. Probably I should add this to the "Helptext generation" page in the documentation. Here's a runnable example: from dataclasses import field, make_dataclass
from typing_extensions import Annotated
import tyro
DynamicDataclass = make_dataclass(
"DynamicDataclass",
[
("field", str, field()),
# (1) Helptext via type annotation.
(
"field_help1",
Annotated[str, tyro.conf.arg(help="Helptext for this field.")],
field(),
),
# (2) Helptext in the same style as expected by HuggingFace's hf_argparser.
(
"field_help2",
str,
field(metadata={"help": "HuggingFace-style helptext."}),
),
],
)
tyro.cli(DynamicDataclass) |
Hi @brentyi, thanks so much for the super helpful pointers!! I didn't realize the metadata help text already existed- I think this is functionally equivalent to the I also tried the encouraged API and it also works- keeps my code a little cleaner too. 😁 Thanks for this super awesome lib! |
Another weird question:
Is it possible to set docstrings programatically for dynamic dataclasses?
A quick look through the source seems to indicate that this isn't possible, since for dataclasses it relies on the source existing, and seems to default to returning None in the case it detects that the object is a dynamic dataclass.
It could be super useful if you could add optional support for docs with dynamic dataclasses, for purposes like dynamically generating schemas from class definitions / function definitions. It's not super elegant but this could be exposed through the
metadata
field ofdataclasses.field
, which according to the docs is meant for 3rd party extensions for dynamic dataclasses.The text was updated successfully, but these errors were encountered: