-
Notifications
You must be signed in to change notification settings - Fork 29
[POC] Improve importlib.metadata usage
#854
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
Draft
mkniewallner
wants to merge
13
commits into
main
Choose a base branch
from
feat/improve-importlib-metadata-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 tasks
6ddf92c to
b5a6eb8
Compare
319b72f to
7fba868
Compare
7fba868 to
cb1d4f1
Compare
Owner
|
Great idea, I love it. Less custom logic on our side sounds like an improvement :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Roughly a POC for now that will most likely get split into multiple PRs in the end, but thought it would be nice to have a first draft, to raise some discussion.
While working towards how to resolve #827, I dug a bit into
importlib.metadatathat we currently use, to see if we have some ways to improve what we do.Python 3.10 introduced
packages_distributionsthat returns a mapping that maps Python module names to the packages that expose them, by reading fromtop_level.txtlike we do. In Python 3.11, the method became even smarter by falling back to reading fromRECORDwhen notop_level.txtis present, also exactly like we do.Updates in
importlib.metadataare upstreamed fromimportlib_metadata, so by requiring>=4.13on Python < 3.11, we could usepackages_distributionsin our codebase for 2 things.First, avoid parsing
top_level.txtandRECORDourselves, and instead simply rely on the method which already does that.Second, and this is the most interesting part, detect more transitive dependencies, as I noticed that when building
Module, we also rely onimportlib.metadataand check if we detect a package <-> module mapping, but by usingmetadatafromimportlib.metadata, which is AFAIK equivalent to only checkingtop_level.txt, and notRECORD, meaning that some dependencies are mistakenly flagged as missing instead of transitive dependencies (like shown in #827).With the changes in this PR,
bs4import in the issue is correctly reported as a transitive dependency, instead of a missing one:Switching to
packages_distributionsalso highlights an issue with our implementation. InModule, we assume that we can only have one package exposing a module, but in fact, it is possible for multiple packages to provide a module (for instance if using namespace packages I believe). Because of that, the report we do might be incomplete, but worse than that, this probably means that we wrongly handle some violations today, as for instance, if a module is provided by 2 packages, where one is a dev dependency and one a production one, we would need to assess the violations based on all packages we find.As mentioned at the top of the description, I'd like to eventually split the PR into smaller pieces:
importlib_metadataon Python < 3.11 and updateDependencyto usepackages_distributions, removing our own logic that reads fromtop_level.txtandRECORDalong the wayModuleto usepackages_distributions, which is more complex to handle since we could now end up with multiple packages when building a module