-
Notifications
You must be signed in to change notification settings - Fork 119
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
Static parsing in autoprofile cannot handle annotated assignments #279
Comments
I've recently fixed this in ubelt and xdoctest. I forgot that the code was ported here too. I came up with the same fix. I'm under quite a bit of pressure at the moment, so a PR to add it here would be helpful. I will merge and release it once it is ready. Unfortunately this is not the easiest thing to write a test for, so just the fix is fine. As an aside: I'm glad someone else ran into this. This was an issue in xdoctest and ubelt (and apparently line-profiler) for several months, and I was really surprised nobody else was submitting issues about |
TL; DR
__editable___*_*finder.py
may have type annotations (e.g. those created bysetuptools > v70.0.0
), which trips the static analysis code used by auto-profiling to locate modules. The fix is a low-hanging fruit though.What happened
A package which I installed locally as editable in my virtual environment can only be profiled with auto-profiling when I am inside the project directory hierarchy. Outside of it, I get something like:
(my_venv) $ python -m pdb -m kernprof --line --outfile <SOME_FILE> --prof-mod <SOME_EDITABLE_PACKAGE> <SOME_BENCKMARK_SCRIPT> -- <SOME_ARGS_FOR_THE_SCRIPT> [...]
The cause
Editable modules are installed via
__editable___<PACKAGE_NAME>_*finder.py
scripts (among other hooks) in thesite-packages
. This is used byline_profiler.autoprofile.util_static._syspath_modname_to_modpath()
to find (byglob.glob()
-ing the file pattern) and process (byline_profiler.autoprofile.util_static._static_parse()
) those finder scripts, thus statically resolving the--prof-module
module names to the appropriate source where they can be loaded for AST manipulation.Specifically,
._static_parse()
is used to locate the definition ofMAPPING
in the finder script, likeNote particularly how the assignment to
MAPPING
is type-annotated – this is a relatively new invention, thesetuptools
commit which added this was only about a month old. However, this has ramifications for._static_parse()
, since the.<locals>.StaticVisitor
it uses only handles non-annotated assignments (parsed toast.Assign
nodes) but not annotated ones (parsed toast.AnnAssign
nodes):Hence the visitor misses the node, and the code falls through to the reporting of the
AttributeError
as seen in the traceback block.A possible solution
Just extend
StaticVisitor
to handle annotated assignments:This probably wouldn't cause performance issues since
._static_parse()
is only invoked once (per finder script) in the entire lifetime of the program, and IDK if there are other edge cases not taken care of or whether it would miraculously break some tests. But the borderline is that it now works on my machine™ so I'm happy.The text was updated successfully, but these errors were encountered: