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 compatibility kludge to work with older packaging #1217

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions changelog/1217.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix compatibility kludge for invalid License-File metadata entries emitted by
build backends to work also with ``packaging`` version 24.0.
25 changes: 17 additions & 8 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,7 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":

# Parse and validate metadata.
meta, unparsed = metadata.parse_email(data)
if unparsed:
raise exceptions.InvalidDistribution(
"Invalid distribution metadata: {}".format(
"; ".join(
f"unrecognized or malformed field {key!r}" for key in unparsed
)
)
)

# setuptools emits License-File metadata fields while declaring
# Metadata-Version 2.1. This is invalid because the metadata
# specification does not allow to add arbitrary fields, and because
Expand All @@ -232,6 +225,22 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
# than 2.4.
if version.Version(meta.get("metadata_version", "0")) < version.Version("2.4"):
meta.pop("license_files", None)
# Support for metadata version 2.4 requires packaging version 24.1
# or later. When parsing metadata with an older packaging, the
# invalid License-File fields are not understood and added to the
# unparsed dictionary. Remove them to avoid triggering the
# following check.
unparsed.pop("license-file", None)

if unparsed:
raise exceptions.InvalidDistribution(
"Invalid distribution metadata: {}".format(
"; ".join(
f"unrecognized or malformed field {key!r}" for key in unparsed
)
)
)

try:
metadata.Metadata.from_raw(meta)
except metadata.ExceptionGroup as group:
Expand Down
Loading