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

Smarter choice when name comes from multiple modules #61

Open
wimglenn opened this issue Aug 27, 2024 · 1 comment
Open

Smarter choice when name comes from multiple modules #61

wimglenn opened this issue Aug 27, 2024 · 1 comment

Comments

@wimglenn
Copy link

wimglenn commented Aug 27, 2024

Reproducer:

$ cat a.py
x = 1

$ cat b.py
from a import x

$ cat c.py
from a import *
from b import *
print(x)

$ PYTHONPATH=. removestar c.py
Warning: c.py: 'x' comes from multiple modules: 'a', 'b'. Using 'b'.
--- original/c.py
+++ fixed/c.py
@@ -1,4 +1,3 @@
-from a import *
-from b import *
+from b import x
 print(x)

I understand why removestar is using b here, it's the most obvious choice in the case that a.x and b.x both exist because the from b import * will override whatever x the from a import * may have put into module namespace. So using b is the safer choice and is guaranteed(?) not to change behavior.

However, in this case they are sourced from the same identical object, a mode (feature flag?) to encourage removestar to see that these are the same x and we should import it directly from the source, a, would be helpful. This would simplify the dependency graph from c -> b -> a to c -> a. My main use-case of removestar is exactly this point - untangling needlessly messy dependency graphs that grew over time in a big monorepo.

Does it sound feasible?

@Saransh-cpp
Copy link
Collaborator

I was looking around to see if this enhancement is trivial - removestar chooses the last/latest module used to import the variable, and to check for the dependency graph, we might need to start a recursive search through all the modules that are being used to import the variables. This will get complex if the variable itself is being * imported in the any of the "parent" modules.

It will be a useful features (given that tools like ruff do not provide any checks for tangled dependency graphs; at least removestar throws a warning), but I will not have time in the next few months to work on this :(

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