From 3ea3455598a7b7e4aeda0a1dbdc4d976c582d953 Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Wed, 22 Jan 2025 10:42:45 +0100 Subject: [PATCH] Fix compatibility kludge to work with older packaging Fixes #1216. --- changelog/1217.bugfix.rst | 2 ++ twine/package.py | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 changelog/1217.bugfix.rst diff --git a/changelog/1217.bugfix.rst b/changelog/1217.bugfix.rst new file mode 100644 index 00000000..46a34e32 --- /dev/null +++ b/changelog/1217.bugfix.rst @@ -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. diff --git a/twine/package.py b/twine/package.py index 831bcdbd..3bd28d0e 100644 --- a/twine/package.py +++ b/twine/package.py @@ -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 @@ -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: