I would like to use entry-points to load alternative implementations (i.e. to dispatch if available metadata is right, but have all information always available for introspection).
Additionally, I would like to make sure that nobody does a costly import by accident, but with a typical entrypoint.load()
in a Python file that may be part of a heavy-weight package, it is easy to get slow imports.
Because of that, I want the entry-point to only point to a bunch of metadata.
During a discussion, the idea came up to make the entry-point not load a Python reference (to something like a dictionary) but instead load all relevant data from a .toml
file which the entry-point points at.
Now I tried this and it works fine! Although the code to actual load the .toml
you need something a bit manual:
def load_entrypoint(ep):
mod, _, filename = ep.value.partition(":")
# `mod` seems to need to be top-level to truly avoid imports
spec = importlib.util.find_spec(mod)
reader = spec.loader.get_resource_reader(spec.name)
with reader.open_resource(filename) as f:
return tomllib.load(f)
(This seems to work, I suspect zipped packages are OK, dunno if there is a niche I may be missing.)
However, importlib_metadata
(new versions) seem to complain already when discovering entry-points (not wrongly, they notice that ep.value
isn’t valid if it has a /
! Of course if there is no /
in the path it won’t notice this but…)
Also, validate-pyproject
also correctly complains.
Question: It seems a bit that this is a highly unusual thought to discover an arbitrary file rather than Python object reference via an entry-point
.
Is there an alternative pattern for something similar to this, does it seem OK besides that it was never really intended, or does this seem like a bad idea best to avoid?