diff --git a/twine/package.py b/twine/package.py index 831bcdbd..d233be08 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,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: