Skip to content

Commit 525cbde

Browse files
committed
Accommodate for invalid metadata produced by setuptools
See pypa/setuptools#4759.
1 parent 70ef763 commit 525cbde

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/test_package.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,34 @@ def test_package_from_unrecognized_file_error():
432432
with pytest.raises(exceptions.InvalidDistribution) as err:
433433
package_file.PackageFile.from_filename(filename, comment=None)
434434
assert "Unknown distribution format" in err.value.args[0]
435+
436+
437+
@pytest.mark.parametrize(
438+
"read_data, filtered",
439+
[
440+
pytest.param(
441+
"Metadata-Version: 2.1\n"
442+
"Name: test-package\n"
443+
"Version: 1.0.0\n"
444+
"License-File: LICENSE\n",
445+
True,
446+
id="invalid License-File",
447+
),
448+
pytest.param(
449+
"Metadata-Version: 2.4\n"
450+
"Name: test-package\n"
451+
"Version: 1.0.0\n"
452+
"License-File: LICENSE\n",
453+
False,
454+
id="valid License-File",
455+
),
456+
],
457+
)
458+
def test_setuptools_license_file(read_data, filtered, monkeypatch):
459+
"""Drop License-File metadata entries if Metadata-Version is less than 2.4."""
460+
monkeypatch.setattr(package_file.wheel.Wheel, "read", lambda _: read_data)
461+
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
462+
463+
package = package_file.PackageFile.from_filename(filename, comment=None)
464+
meta = package.metadata_dictionary()
465+
assert filtered != ("license_file" in meta)

twine/package.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, TypedDict
2222

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

2627
from twine import exceptions
@@ -221,6 +222,16 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
221222
)
222223
)
223224
)
225+
# setuptools emits License-File metadata fields while declaring
226+
# Metadata-Version 2.1. This is invalid because the metadata
227+
# specification does not allow to add arbitrary fields, and because
228+
# the semantic implemented by setuptools is different than the one
229+
# described in PEP 639. However, rejecting these packages would be
230+
# too disruptive. Drop License-File metadata entries from the data
231+
# sent to the package index if the declared metadata version is less
232+
# than 2.4.
233+
if version.Version(meta.get("metadata_version", "0")) < version.Version("2.4"):
234+
meta.pop("license_files", None)
224235
try:
225236
metadata.Metadata.from_raw(meta)
226237
except metadata.ExceptionGroup as group:

0 commit comments

Comments
 (0)