Skip to content

Commit ab0a470

Browse files
committed
Accommodate for invalid metadata produced by setuptools
See pypa/setuptools#4759.
1 parent 5183b6c commit ab0a470

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/test_package.py

+31
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,34 @@ def test_malformed_from_file(monkeypatch):
466466
def test_package_from_egg():
467467
filename = "tests/fixtures/twine-3.3.0-py3.9.egg"
468468
package_file.PackageFile.from_filename(filename, comment=None)
469+
470+
471+
@pytest.mark.parametrize(
472+
"read_data, filtered",
473+
[
474+
pytest.param(
475+
"Metadata-Version: 2.1\n"
476+
"Name: test-package\n"
477+
"Version: 1.0.0\n"
478+
"License-File: LICENSE\n",
479+
True,
480+
id="invalid License-File",
481+
),
482+
pytest.param(
483+
"Metadata-Version: 2.4\n"
484+
"Name: test-package\n"
485+
"Version: 1.0.0\n"
486+
"License-File: LICENSE\n",
487+
False,
488+
id="valid License-File",
489+
),
490+
],
491+
)
492+
def test_setuptools_license_file(read_data, filtered, monkeypatch):
493+
"""Drop License-File metadata entries if Metadata-Version is less than 2.4."""
494+
monkeypatch.setattr(package_file.wheel.Wheel, "read", lambda _: read_data)
495+
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
496+
497+
package = package_file.PackageFile.from_filename(filename, comment=None)
498+
meta = package.metadata_dictionary()
499+
assert filtered != ("license_files" in meta)

twine/package.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
2222

2323
from packaging import metadata
24+
from packaging import version
2425
from rich import print
2526

2627
from twine import bdist
@@ -142,6 +143,16 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
142143
)
143144
)
144145
)
146+
# setuptools emits License-File metadata fields while declaring
147+
# Metadata-Version 2.1. This is invalid because the metadata
148+
# specification does not allow to add arbitrary fields, and because
149+
# the semantic implemented by setuptools is different than the one
150+
# described in PEP 639. However, rejecting these packages would be
151+
# too disruptive. Drop License-File metadata entries from the data
152+
# sent to the package index if the declared metadata version is less
153+
# than 2.4.
154+
if version.Version(meta.get("metadata_version", "0")) < version.Version("2.4"):
155+
meta.pop("license_files", None)
145156
try:
146157
metadata.Metadata.from_raw(meta)
147158
except metadata.ExceptionGroup as group:

0 commit comments

Comments
 (0)