Skip to content
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

Fix incompatibility with importlib-metadata 8 #2975

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2974.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash when pdm is used with `importlib-metadata` version 8.0.
24 changes: 17 additions & 7 deletions src/pdm/cli/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ def parse_comma_separated_string(
return items


def _get_metadata_key(dist: im.Distribution, key: str) -> str | None:
"""Get a key from metadata, or return None if it is not found or 'UNKNOWN'."""
# Starting with importlib-metadata 8.0, KeyError is thrown for missing keys.
try:
value = dist.metadata[key]
except KeyError:
return None
else:
if not value or value == "UNKNOWN":
frostming marked this conversation as resolved.
Show resolved Hide resolved
return None
return value


class Listable:
"""Wrapper makes sorting and exporting information about a Distribution
easier. It also retrieves license information from dist-info metadata.
Expand All @@ -322,20 +335,17 @@ class Listable:
def __init__(self, dist: im.Distribution, groups: set[str]):
self.dist = dist

self.name: str | None = dist.metadata["Name"]
self.name = _get_metadata_key(dist, "Name")
self.groups = "|".join(groups)

self.version: str | None = dist.metadata["Version"]
self.version = None if self.version == "UNKNOWN" else self.version
self.version = _get_metadata_key(dist, "Version")

self.homepage: str | None = dist.metadata["Home-Page"]
self.homepage = None if self.homepage == "UNKNOWN" else self.homepage
self.homepage = _get_metadata_key(dist, "Home-Page")

# If the License metadata field is empty or UNKNOWN then try to
# find the license in the Trove classifiers. There may be more than one
# so generate a pipe separated list (to avoid complexity with CSV export).
self.licenses: str | None = dist.metadata["License"]
self.licenses = None if self.licenses == "UNKNOWN" else self.licenses
self.licenses = _get_metadata_key(dist, "License")

# Sometimes package metadata contains the full license text.
# e.g. license = { file="LICENSE" } in pyproject.toml
Expand Down
7 changes: 6 additions & 1 deletion src/pdm/models/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,12 @@ def metadata(self) -> im.Distribution:
if not self.candidate.version:
self.candidate.version = result.version
if not self.candidate.requires_python:
self.candidate.requires_python = cast(str, result.metadata["Requires-Python"] or "")
# Starting in importlib_metadata 8.0, KeyError is thrown if the key is missing
try:
requires_python = result.metadata["Requires-Python"] or ""
except KeyError:
requires_python = ""
frostming marked this conversation as resolved.
Show resolved Hide resolved
self.candidate.requires_python = requires_python
self._metadata = result
return self._metadata

Expand Down