-
Notifications
You must be signed in to change notification settings - Fork 52
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
Tidy local imports (PyInf#11558) #273
Comments
I've been asked to come back to this. My feeling with the architecture of pyflyby is that it's going to be complex, and just estimating the work would need significant work to figure out what is needed. I'll try to look into in over the next few weeks to see what I can do, and try to come up with a plan/estimate. |
Hi @Carreau, do you have an estimate how much work this would be? |
I haven't been able to look into it, I'm trying to get the pyinstruments fixes first. I'll put that higher on the priority list. |
I'm looking into this.
I believe this is already the case; at least on some example I tried it works.
I'll edit the original request to split request 2, into 2a and 2b. |
More on this also for myself once I resume the work later on.
Taking the above example, detecting that
With the current hardcoded "Assume sys is unused"; Pyflyby now detects it (at least in debug logs) but warns that
Removing the local I'm also a bit concerned if a local import is made to shadow a variable how this will affects the result.
I haven't thought on how to take care of this.
|
Modifying non-local importsI've looked into modifying non-global import more in depth. Pyflyby SourceToSource transformer has only been designed to only handle top-level refactoring; And this is a deep assumption in pyflyby – the finest level of granularity is I think that updating pyflyby to allow non-top-level source updates is quite complicated; likely a multi-month project; and more of a complete rewrite than just an update. There exists a few more recent frameworks that might allow what you wish;
In the options I don't really like;
Marking unnecessary local importsAs we talked about last Wednesday, with pyflyby architecture it's hard to prioritize local imports as we don't keep references to the ast while we are walking it; and so in the case:
Right now the importChecker keep a set of used/unused imports; I think I might be able to change that to a counter (and store a definite usage counter (sys.argv), and potential usage counter (global Still keeping the caveat that right now it's unclear if we would be able to automatically remove the non-local imports, or which implementation choice we'd like. I'll anyway cleanup the updates I have locally, and send a PR that will be a base for the detection of unused local imports. |
Removing non top-level import would potentially have another side-effect; pyflyby currently make the first import in the following as unused:
As it does not understand control structures; and currently rely on the fact that it's not top-level to not remove it. I'll have to adapt pyflyby to better track the context in which an import is marked as unused. |
Thanks for investigating & overview. |
Just for openness, I will be off work from early March to September for personal reasons if all goes well this year, we are working on finding someone else that could both scope and does the work, as it seem unadvisable for me to take something like this at this time. |
Currently tidy-imports only optimizes global imports. How difficult would it be to also optimize "local" imports, i.e., imports within a function? Example:
Here we should
import sys
is unused infoo
and suggest to remove itimport os
is present both at global as well as local scope.a. I think here it's correct to remove the global import since the only usage is in a function where
os
is imported locally.b. But in general if there's a global import not covered by local imports everywhere we should remove the local import.
Can you estimate how much work this would be?
The text was updated successfully, but these errors were encountered: