Open yuvval opened 2 years ago
Hello there @yuvval !
Is this specific to dataclasses? Or to Simple-parsing?
Dataclasses generate a str that is then evaled, so thats probably the issue here.
Hi Fabrice! Thanks for helping.
Looks like it is related to simple-parsing: It works when I removed the calls for simple parsing, and replaced them with just getting an instance of the dataclasses.
For that I used the following code:
from dataclasses import dataclass
from time import sleep
@dataclass
class NestedArgs():
nested1: str = 'nested1'
@dataclass
class Opts():
nested: NestedArgs
arg1: bool = False
arg2: int = 4
@profile
def func_to_line_prof():
sleep(0.01)
sleep(1)
sleep(3)
def main():
o = Opts(NestedArgs())
func_to_line_prof()
if __name__ == '__main__':
main()
FYI: The issue is happening when simple-parsing tries to fetch the docstring of a given field on a dataclass, in order to create the value that gets passed to parser.add_argument(f"--{field.name}", ..., help=<this value here>)
I'm not sure how to fix this, since what seems to be happening is that inspect
is incapable of finding the source code of the given class. Could this be due to the line profiler manipulating the code objects somehow?
In any case, I don't think I have an easy fix for this, apart from perhaps adding a help string for every field using the field
function from simple_parsing.helpers:
@dataclass
class Foo:
""" Foo docstring """
a: int = field(default=123, help="Help string for A")
That way, simple-parsing won't have to look for the source file of the dataclass when trying to construct the help string for the field, since it's already provided.
Describe the bug When calling a script with kernprof (line_profiler), it breaks if simple parsing uses nesting. There is no issue if nesting is not used.
To Reproduce
tmp.py
On commandline (bash):
Expected behavior Script will run and func_to_line_prof() will get profiled.
Actual behavior An exception is raised
Desktop (please complete the following information):