You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ 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?
The text was updated successfully, but these errors were encountered:
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 :(
Reproducer:
I understand why
removestar
is usingb
here, it's the most obvious choice in the case thata.x
andb.x
both exist because thefrom b import *
will override whateverx
thefrom a import *
may have put into module namespace. So usingb
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 fromc -> b -> a
toc -> 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?
The text was updated successfully, but these errors were encountered: