Trace icon indicating copy to clipboard operation
Trace copied to clipboard

Programmatically update Docstring

Open ztyree42 opened this issue 8 months ago • 4 comments

Hi,

I want to use Trace to build an application to let non-engineers optimized code for certain tasks. Ideally I would like to have an f-string in place of the docstring for a number of bundled functions that are methods on classes decorated by @model but of course python requires docstrings be a string literal. I'm REALLY trying to avoid resorting to a templating library for this. Currently I am trying to update the docstring of a number of bundled functions through some combination of bundled_function.description bundled_function.info['doc'] bundled_function._fun.__doc__ but so far no matter what I do I can't see the new docstring in the parameters of either the function node or in the associated optimizers.

Do you all have a suggestion of how I could implement a templated docstring? For reference here's a SUPER minimal example of what I mean:

@bundle some_func(input_data):
f"""input_data is {data_description}. 
You need to {task_description}. 
Make sure you don't {caveats_description}.
"""
    pass

ztyree42 avatar Apr 08 '25 13:04 ztyree42

Update @allenanie in order to enable updating the doc-string programmatically I'm currently doing this:

FOO = 'bar'
example_doc_fstring = f"""Example doc-string with element FOO={bar} not known until runtime."""

@bundle(trainable=True)
def example_func(some_input):
    """<placeholder docstring>"""
    pass

example_func.__doc__ = example_doc_fstring
example_func = bundle(trainable=True)(example_func)
example_func.info['source'] = example_func.info['source'].replace('<placeholder docstring>', example_func.info['doc'])
example_func.parameter = ParameterNode(
    example_func.info["source"],
    name=None,
    constraint="The code should start with:\n" + example_func.info['source'].strip('pass').rstrip(),
)

I think allowing the doc-strings to by defined at runtime opens up a lot of possibilities. If you want to support this feature I think the main effort would be to add a bit more regular expression logic in the ParamterNode constructor so that source code and doc-string could be disambiguated.

Alternatively the @bundle decorator could be modified to use the information provided in the description argument to overwrite the docstring in the wrapped function.

Probably other options. If one of these seems preferable and this is a feature you want to support I'd be happy to make a PR for it.

Great work on this btw, I'm getting a lot of mileage out of it!

ztyree42 avatar Apr 08 '25 20:04 ztyree42

@ztyree42 That's a good suggestion. Do you want to create a fork on this and open a PR? I can work with you.

chinganc avatar Apr 22 '25 18:04 chinganc

We are also working on a community contribution guideline to allow easier iteration.

allenanie avatar Apr 23 '25 19:04 allenanie

@chinganc sure, i'll try to get to it this weekend, i'll tag you on the PR, thanks

ztyree42 avatar May 02 '25 12:05 ztyree42