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

Tidy local imports (PyInf#11558) #273

Open
ArvidJB opened this issue Nov 21, 2023 · 3 comments
Open

Tidy local imports (PyInf#11558) #273

ArvidJB opened this issue Nov 21, 2023 · 3 comments
Assignees

Comments

@ArvidJB
Copy link
Collaborator

ArvidJB commented Nov 21, 2023

Currently tidy-imports only optimizes global imports. How difficult would it be to also optimize "local" imports, i.e., imports within a function? Example:

$ cat foo.py
import os

def foo():
    import os
    import sys
    return os.getenv('FOO')
$ tidy-imports foo.py
[PYFLYBY] /tmp/foo.py: removed unused 'import os'
[PYFLYBY] /tmp/foo.py: added mandatory 'from __future__ import annotations'
--- /tmp/foo.py   2023-11-21 16:37:32.473138334 -0500
+++ /tmp/tmpgciyp7p6    2023-11-21 16:38:48.351697332 -0500
@@ -1,4 +1,5 @@
-import os
+from __future__ import annotations
+

 def foo():
     import os

Replace /tmp/foo.py? [y/N]

Here we should

  1. detect that import sys is unused in foo and suggest to remove it
  2. detect that import os is present both at global as well as local scope. I think here it's correct to remove the global import since the only usage is in a function where os is imported locally. 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?

@ArvidJB ArvidJB changed the title Tidy local imports Tidy local imports (PyInf#11558) Dec 1, 2023
@Carreau
Copy link
Collaborator

Carreau commented Aug 6, 2024

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.

@ArvidJB
Copy link
Collaborator Author

ArvidJB commented Sep 5, 2024

Hi @Carreau, do you have an estimate how much work this would be?

@Carreau Carreau self-assigned this Sep 11, 2024
@Carreau
Copy link
Collaborator

Carreau commented Sep 11, 2024

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.

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