Skip to content
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

Closed
tovacinni opened this issue Dec 29, 2022 · 3 comments
Closed

Setting docstrings for dynamic dataclasses #25

tovacinni opened this issue Dec 29, 2022 · 3 comments

Comments

@tovacinni
Copy link

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 of dataclasses.field, which according to the docs is meant for 3rd party extensions for dynamic dataclasses.

@tovacinni
Copy link
Author

tovacinni commented Dec 29, 2022

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

@brentyi
Copy link
Owner

brentyi commented Dec 29, 2022

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. By modifying the annotated type: tyro.conf.arg() can be used to add helptext to any arbitrary field. (API reference, relevant example)
  2. Something I added to support config dataclasses written for HuggingFace models is parsing a "help" field from dataclass metadata; here's the relevant bit in the code.

(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)

@tovacinni
Copy link
Author

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 docs metadata I put in the MR (so I don't think the MR approach has any advantages). I quickly tried it on my side and seems to work great.

I also tried the encouraged API and it also works- keeps my code a little cleaner too. 😁 Thanks for this super awesome lib!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants