From 5a783b797d9fbfeb6e8807625b1c52f2d56464cb Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Tue, 17 Dec 2024 14:53:07 +0100 Subject: [PATCH] Do not send empty fields in the upload form data No behavior changes intended. Just cleaning up some loose ends. --- twine/package.py | 26 ++++++++++++++++---------- twine/repository.py | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/twine/package.py b/twine/package.py index 5d8db29a..831bcdbd 100644 --- a/twine/package.py +++ b/twine/package.py @@ -152,13 +152,13 @@ class PackageMetadata(TypedDict, total=False): license_file: List[str] # Additional metadata - comment: Optional[str] + comment: str pyversion: str filetype: str gpg_signature: Tuple[str, bytes] attestations: str md5_digest: str - sha256_digest: Optional[str] + sha256_digest: str blake2_256_digest: str @@ -256,14 +256,23 @@ def metadata_dictionary(self) -> PackageMetadata: # names are computed but they can only be valid key names. data[field] = value # type: ignore[literal-required] - # override name with safe name + # Override name with safe name. data["name"] = self.safe_name - # file content + + # File content. data["pyversion"] = self.python_version data["filetype"] = self.filetype - # additional meta-data - data["comment"] = self.comment - data["sha256_digest"] = self.sha2_digest + + # Additional meta-data: some of these fileds may not be set. Some + # package repositories do not allow null values, so this only sends + # non-null values. In particular, FIPS disables MD5 and Blake2, making + # the digest values null. See https://github.com/pypa/twine/issues/775 + + if self.comment is not None: + data["comment"] = self.comment + + if self.sha2_digest is not None: + data["sha256_digest"] = self.sha2_digest if self.gpg_signature is not None: data["gpg_signature"] = self.gpg_signature @@ -271,9 +280,6 @@ def metadata_dictionary(self) -> PackageMetadata: if self.attestations is not None: data["attestations"] = json.dumps(self.attestations) - # FIPS disables MD5 and Blake2, making the digest values None. Some package - # repositories don't allow null values, so this only sends non-null values. - # See also: https://github.com/pypa/twine/issues/775 if self.md5_digest: data["md5_digest"] = self.md5_digest diff --git a/twine/repository.py b/twine/repository.py index 3b5f68a8..cc8b01de 100644 --- a/twine/repository.py +++ b/twine/repository.py @@ -79,7 +79,7 @@ def _convert_metadata_to_list_of_tuples( elif isinstance(value, (list, tuple)): data_to_send.extend((key, item) for item in value) else: - assert isinstance(value, str) or value is None + assert isinstance(value, str) data_to_send.append((key, value)) return data_to_send