Skip to content

Smarter choice when name comes from multiple modules #61

Open
@wimglenn

Description

@wimglenn

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions