-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
importlib.metadata.Distribution equality check not working #107220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report. Happy to explore the issue here. I'm not at all confident that equality by name is correct, because it's possible for multiple distributions of the same name to exist in the environment. For example:
This example uses pip-run to install two different versions of In this case, I wouldn't expect these two distributions to compare equal, even though they have the same name. Moreover, if two distributions had the same name and version but were located in different places on Moreover, metadata providers are expected to implement their own Another thing to consider - should a distribution compare equal to a string of the name? In one example, you imply that's expected: entry_points(group='console_scripts', dist='pip') # empty EntryPoints list That suggests that I agree that entry point selection is a compelling use case for such a comparison. I wouldn't characterize the described behavior as a bug, but rather an unaddressed feature. |
Thanks for looking at this @jaraco
Seems weird to me, at least I have never seen or heard of anything like that.
Currently |
To be constructive, I set up a quick PR with what I would've wished to see in the docs at the time when I was transitioning our package from |
Sorry for the delay in reviewing this. Can you share more about how |
What we did (still do actually) with from pkg_resources import load_entry_point
load_entry_point('pip', 'console_scripts', 'pip') What I thought would do the trick with import importlib.metadata
# doesn't work, returns an empty list
importlib.metadata.entry_points(dist='pip', group='console_scripts', name='pip')[0].load() Only way I could get this replicated with import importlib.metadata
list(ep for ep in importlib.metadata.entry_points(group='console_scripts', name='pip') if ep.dist.name == 'pip')[0].load() Like mentioned above, my main issue with it is that |
It's been quite a year, and I'm just now digging deep enough into my emails to follow up on this. Sorry for the delay. I can't recall, but I may have failed to connect the dots on the PR until just now. Thanks for that proposal. Thinking about the use-case, I believe the recommended approach for getting entry points for a specific distribution would be done like so: importlib.metadata.distribution('pip').entry_points.select(name='pip', group='console_scripts') e.g.:
I think I agree we should update the guidance or maybe issue warnings to reduce the risk of this unintended use, though I'd also consider adding support for it if we can figure out how richly to do so. |
Another option, now that I think about it, is to simply query for the "pip" "console_scripts":
And not bother filtering by distribution at all, since it's unlikely another package would implement the pip console script (and if they did, you have bigger problems). |
Bug report
During migrating our package from
pkg_resources
to usingimportlib.metadata
I encountered some weirdness when it comes to usingimportlib.metadata.Distribution
objects. Specifically, it seems to lack a custom__eq__
operator, simply inheritingobject.__eq__
. In consequence checking distributions for equality seems to just always fail.... which in turn makes it impossible to use
dist
kwarg in entry point selection and users have to fall back to manually comparing distribution plain textname
attribute (or something similar / more sophisticated):I think comparing distributions for equality should be fixed, maybe even allowing comparison with a plain string containing the distribution's name (e.g.
"pip"
) might make sense (although I'm not aware of potential drawbacks / conflicts that might happen in case there might be multiple distributions with same "name" which intuitively seems odd to me).I believe this is the right place for this report, I'm very sorry if it isn't.
Your environment
conda env:
miniconda base env:
Linked PRs
The text was updated successfully, but these errors were encountered: