Skip to content

Commit

Permalink
Fix compatibility kludge to work with older packaging
Browse files Browse the repository at this point in the history
Fixes pypa#1216.
  • Loading branch information
dnicolodi committed Jan 22, 2025
1 parent 4406034 commit 5c7dcb9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
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.
24 changes: 16 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,21 @@ 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 lalet. When parsing invalid metadata with an older packagign,
# the invalid License-File fileds are not understood and added to
# the unparsed dictionary.
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

0 comments on commit 5c7dcb9

Please sign in to comment.