Skip to content

Commit

Permalink
Accommodate for invalid metadata produced by setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolodi committed Dec 1, 2024
1 parent c71f0f9 commit 8bc7b2a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,34 @@ def test_malformed_from_file(monkeypatch):
def test_package_from_egg():
filename = "tests/fixtures/twine-3.3.0-py3.9.egg"
package_file.PackageFile.from_filename(filename, comment=None)


@pytest.mark.parametrize(
"read_data, filtered",
[
pytest.param(
"Metadata-Version: 2.1\n"
"Name: test-package\n"
"Version: 1.0.0\n"
"License-File: LICENSE\n",
True,
id="invalid License-File",
),
pytest.param(
"Metadata-Version: 2.4\n"
"Name: test-package\n"
"Version: 1.0.0\n"
"License-File: LICENSE\n",
False,
id="valid License-File",
),
],
)
def test_setuptools_license_file(read_data, filtered, monkeypatch):
"""Drop License-File metadata entries if Metadata-Version is less than 2.4."""
monkeypatch.setattr(package_file.wheel.Wheel, "read", lambda _: read_data)
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"

package = package_file.PackageFile.from_filename(filename, comment=None)
meta = package.metadata_dictionary()
assert filtered != ("license_files" in meta)
11 changes: 11 additions & 0 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Any, Dict, List, NamedTuple, Optional, Tuple

from packaging import metadata
from packaging import version
from rich import print

from twine import bdist
Expand Down Expand Up @@ -142,6 +143,16 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
)
)
)
# 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
# the semantic implemented by setuptools is different than the one
# described in PEP 639. However, rejecting these packages would be
# too disruptive. Drop License-File metadata entries from the data
# sent to the package index if the declared metadata version is less
# than 2.4.
if version.Version(meta.get("metadata_version", "0")) < version.Version("2.4"):
meta.pop("license_files", None)
try:
metadata.Metadata.from_raw(meta)
except metadata.ExceptionGroup as group:
Expand Down

0 comments on commit 8bc7b2a

Please sign in to comment.