high-energy-55500
04/06/2022, 2:36 PMimportlib
and pants detecting dependencies:
I’ve got a class which dynamically imports another class using importlib:
# importer.py
import importlib
import classes
class Importer:
def __init__(self, name):
source_module = importlib.import_module(f"{classes.__package__}.s_{name}")
class_name = inflection.camelize(name)
self.source_parser = getattr(source_module, f"Class{class_name}")
here’s a simplified view of what the code looks like
project
| BUILD
| __init__.py
│ importer.py
│
└───classes
| | BUILD
│ │ __init__.py
| | s_bar.py
| | s_baz.py
│ │ s_foo.py
| | ...
the issue is that pants can’t accurately infer that all of the modules under classes/
are needed by the Importer
class. It is obviously able to do so correctly if i explicitly import s_bar, s_baz, s_foo...
, but there are a lot of classes and that feels redundant. I’m already using classes.__package__
instead of simply using a hardcoded path string in the hopes that it would resolve the issue, but it looks like it only imports classes.__init__
. 😞
what’s the right way to solve this?hundreds-father-404
04/06/2022, 3:25 PMhigh-energy-55500
04/06/2022, 4:00 PMimporter.py
also have classes/
as a dependency, which isn’t correct. ideally only importer.py
would be dependent on classes/
, but if there’s no easy solution then this is perfectly suitable.
we use this pattern a little but probably not enough to build a plugin for. good to know that the option exists though. 🙂
p.s.: i hope you used speak-to-text instead of typing those messages out, people should try to rest when recovering from a fracture 😛hundreds-father-404
04/06/2022, 4:02 PMhappy-kitchen-89482
04/06/2022, 5:36 PMoverrides=
to set the explicit deps just on the files that actually need them, rather than on all files in the target generatorhigh-energy-55500
04/06/2022, 6:38 PMpython_sources(
overrides={
"importer.py": {
"dependencies": ["./classes"],
},
},
)
happy-kitchen-89482
04/06/2022, 7:05 PM./
prefix, but that's the idea