not directly related to Pants itself, but still ve...
# general
f
not directly related to Pants itself, but still very important for large Pants repos with Python sources. I wonder if there's any tooling to identify unnecessary transitive imports in Python code? In practice,
A
depends on
B
because
B
provides
foo
, however, the
foo
is declared in
C
, so now
A
depends both on
B
and
C
(transitively).
A -> B -> C
is what we have but we could have
A -> C
and
B -> C
. 🧵
1
Copy code
# a.py
from b import foo
# this should really be
# from c import foo
Copy code
# b.py
from c import foo
Copy code
# c.py
foo = "bar"
I've searched online for a while and found nothing. Pants dependency graph won't be of help as it doesn't currently expose imported items (i.e. individual code elements) - one would need to check where the variable is declared anyway (which is not trivial). This issue is not immediately harmful -- if
foo
is not longer in
B
(but
A
still depends on it), then this would be likely caught during the tests. But it does lead to unnecessary dependencies which may be harmful if
B
depends on ton of other stuff, in particular.
w
I thought this was a mypy check, or maybe a pyright check? One of the tools I’ve used tells me to import from the “source” where possible
b
Yeah mypy does this if enabled.
w
👆 1
f
ah that one, they called it "re-export" whereas I was searching for transitive import
let me give it a try