From 71346e0a942649fba8e17c67805a4dbe5e67420f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 25 Jun 2024 13:22:42 -0400 Subject: [PATCH] Only emit the supported metadata versions notice for older pkginfo versions. --- pyproject.toml | 1 + twine/package.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 77bbd729..84125474 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ dependencies = [ "keyring >= 15.1", "rfc3986 >= 1.4.0", "rich >= 12.0.0", + "packaging", # workaround for #1116 "pkginfo < 1.11", diff --git a/twine/package.py b/twine/package.py index 4d4bed39..885b5dad 100644 --- a/twine/package.py +++ b/twine/package.py @@ -33,6 +33,7 @@ ) import importlib_metadata +import packaging.version import pkginfo from rich import print @@ -134,14 +135,15 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile": f.capitalize() for f in ["name", "version"] if not getattr(meta, f) ] if missing_fields: - raise exceptions.InvalidDistribution( - "Metadata is missing required fields: " - f"{', '.join(missing_fields)}." - # TODO: Remove this section after requiring pkginfo>=1.11 - "\nMake sure the distribution includes the files where those fields " - "are specified, and is using a supported Metadata-Version: " - f"{', '.join(supported_metadata)}." - ) + msg = f"Metadata is missing required fields: {', '.join(missing_fields)}." + if cls._pkginfo_before_1_11(): + msg += ( + "\n" + "Make sure the distribution includes the files where those fields " + "are specified, and is using a supported Metadata-Version: " + f"{', '.join(supported_metadata)}." + ) + raise exceptions.InvalidDistribution(msg) py_version: Optional[str] if dtype == "bdist_egg": @@ -163,6 +165,11 @@ def _is_unknown_metadata_version( NMV = getattr(pkginfo.distribution, "NewMetadataVersion", None) return any(warning.category is NMV for warning in captured) + @staticmethod + def _pkginfo_before_1_11() -> bool: + ver = packaging.version.Version(importlib_metadata.version("pkginfo")) + return ver < packaging.version.Version("1.11") + def metadata_dictionary(self) -> Dict[str, MetadataValue]: """Merge multiple sources of metadata into a single dictionary.