From 3449942cdf43a957659c2977638ffdb07ac6e23c Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Tue, 3 Dec 2024 17:08:01 +0100 Subject: [PATCH] Use "source" as python-version for sdist uploads Although not setting the "pyversion" field in the upload data works, and despite it not being used for much, this is what PyPI (and most likely other indexes) expects. This will simplify follow-up patches. --- changelog/1191.misc.txt | 3 +++ tests/test_package.py | 6 ++++++ twine/package.py | 6 ++++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelog/1191.misc.txt diff --git a/changelog/1191.misc.txt b/changelog/1191.misc.txt new file mode 100644 index 00000000..a9edc539 --- /dev/null +++ b/changelog/1191.misc.txt @@ -0,0 +1,3 @@ +- Use ``"source"`` instead of ``None`` as ``pyversion`` for ``sdist`` + uploads. This is what PyPI (and most likely other package indexes) + expects. diff --git a/tests/test_package.py b/tests/test_package.py index be3d9540..37125741 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -466,3 +466,9 @@ 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) + + +def test_package_from_sdist(): + filename = "tests/fixtures/twine-1.5.0.tar.gz" + p = package_file.PackageFile.from_filename(filename, comment=None) + assert p.python_version == "source" diff --git a/twine/package.py b/twine/package.py index 8bcec211..2c3a8f7d 100644 --- a/twine/package.py +++ b/twine/package.py @@ -157,7 +157,6 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile": ) raise exceptions.InvalidDistribution(msg) - py_version: Optional[str] if dtype == "bdist_egg": (dist,) = importlib_metadata.Distribution.discover(path=[filename]) py_version = dist.metadata["Version"] @@ -165,8 +164,11 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile": py_version = cast(wheel.Wheel, meta).py_version elif dtype == "bdist_wininst": py_version = cast(wininst.WinInst, meta).py_version + elif dtype == "sdist": + py_version = "source" else: - py_version = None + # This should not be reached. + raise ValueError return cls( filename, comment, cast(CheckedDistribution, meta), py_version, dtype