https://pantsbuild.org/ logo
#general
Title
# general
f

fresh-cat-90827

04/11/2023, 5:19 PM
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

wide-midnight-78598

04/11/2023, 5:26 PM
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

bitter-ability-32190

04/11/2023, 5:29 PM
Yeah mypy does this if enabled.
w

wide-midnight-78598

04/11/2023, 5:33 PM
👆 1
f

fresh-cat-90827

04/11/2023, 5:34 PM
ah that one, they called it "re-export" whereas I was searching for transitive import
let me give it a try
2 Views