-
Notifications
You must be signed in to change notification settings - Fork 18
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
chore: use package_distributions
to get top levels
#861
Draft
mkniewallner
wants to merge
10
commits into
main
Choose a base branch
from
chore/use-package-distributions
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66b41b9
test(functional): test package normalization
mkniewallner 3025a3f
chore(deps): require `importlib-metadata` on < 3.11
mkniewallner 13b3dcc
chore: add compat module to import `importlib.metadata`
mkniewallner 9347454
chore(ruff): prevent usage of `importlib.metadata`
mkniewallner 8dab104
refactor(dependency): use `compat.importlib_metadata`
mkniewallner f29d03b
refactor(module): use `compat.importlib_metadata`
mkniewallner aaa30e4
refactor(cli): use `compat.importlib_metadata`
mkniewallner 3301a3a
chore: add functions for packages <-> distributions
mkniewallner adec5e8
refactor(dependency): use `get_packages_from_distribution`
mkniewallner 0640431
test(cli): add tests for `display_deptry_version`
mkniewallner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
|
||
# Although `importlib.metadata` is available before Python 3.11, we benefit from using `importlib_metadata` package | ||
# on Python < 3.11 because it exposes `packages_distributions` function that we use in the codebase. Python 3.10 also | ||
# has this function, but there are features we need in it that are only available in Python >= 3.11. So by using | ||
# `importlib_metadata`, we benefit from those features for all Python versions we support. | ||
if sys.version_info >= (3, 11): | ||
import importlib.metadata as importlib_metadata | ||
else: | ||
import importlib_metadata # pragma: no cover | ||
|
||
|
||
__all__ = ("importlib_metadata",) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
from __future__ import annotations | ||||||
|
||||||
import re | ||||||
from collections import defaultdict | ||||||
from functools import lru_cache | ||||||
|
||||||
from deptry.compat import importlib_metadata | ||||||
|
||||||
|
||||||
@lru_cache(maxsize=None) | ||||||
def normalize_distribution_name(name: str) -> str: | ||||||
""" | ||||||
Apply name normalization on distribution name, per https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization. | ||||||
""" | ||||||
return re.sub(r"[-_.]+", "-", name).lower() | ||||||
|
||||||
|
||||||
@lru_cache(maxsize=1) | ||||||
def get_packages_normalized_distributions() -> dict[str, set[str]]: | ||||||
""" | ||||||
Return a mapping of top-level packages to their normalized distributions. | ||||||
Cache ensures that we only build this mapping once, since it should not change during the invocation of deptry. | ||||||
""" | ||||||
return { | ||||||
package: {normalize_distribution_name(distribution) for distribution in distributions} | ||||||
for package, distributions in importlib_metadata.packages_distributions().items() | ||||||
} | ||||||
|
||||||
|
||||||
@lru_cache(maxsize=1) | ||||||
def get_normalized_distributions_packages() -> dict[str, set[str]]: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
Return a mapping of normalized distributions to their top-level packages. | ||||||
Cache ensures that we only build this mapping once, since it should not change during the invocation of deptry. | ||||||
""" | ||||||
distributions_packages: dict[str, set[str]] = defaultdict(set) | ||||||
|
||||||
for package, distributions in get_packages_normalized_distributions().items(): | ||||||
for distribution in distributions: | ||||||
distributions_packages[distribution].add(package) | ||||||
|
||||||
return dict(distributions_packages) | ||||||
|
||||||
|
||||||
def get_distributions_from_package(name: str) -> set[str] | None: | ||||||
""" | ||||||
Retrieve the distributions provided by the package, if any. | ||||||
""" | ||||||
return get_packages_normalized_distributions().get(name) | ||||||
|
||||||
|
||||||
def get_packages_from_distribution(name: str) -> set[str] | None: | ||||||
""" | ||||||
Normalize the distribution and retrieve the packages it provides, if any. | ||||||
""" | ||||||
return get_normalized_distributions_packages().get(normalize_distribution_name(name)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance it is a bit confusing to understand the difference between this function and
get_normalized_distributions_packages
. Maybe we can make their names slightly more verbose? Or add a small example output in their docstring?