From 5688c24a6177e6da9371e523e5bf0ea68e0bb195 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Wed, 4 Nov 2026 08:21:48 +0000 Subject: [PATCH] fix pylock.toml export dropping wheels when the sdist sorts between wheels --- src/poetry_plugin_export/exporter.py | 21 ++----- tests/test_exporter_pylock_toml.py | 82 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 15 deletions(-) diff --git a/src/poetry_plugin_export/exporter.py b/src/poetry_plugin_export/exporter.py index 7884e3e..66983b6 100644 --- a/src/poetry_plugin_export/exporter.py +++ b/src/poetry_plugin_export/exporter.py @@ -1,7 +1,6 @@ from __future__ import annotations import contextlib -import itertools import urllib.parse from datetime import datetime @@ -395,24 +394,16 @@ def add_file_info( package.source_reference or "PyPI", ).files } - artifacts = { - k: list(v) - for k, v in itertools.groupby( - package.files, - key=( - lambda x: ( - "wheel" if x["file"].endswith(".whl") else "sdist" - ) - ), - ) - } - - sdist_files = list(artifacts.get("sdist", [])) + sdist_files = [ + f for f in package.files if not f["file"].endswith(".whl") + ] for sdist in sdist_files: sdist_table = inline_table() data["sdist"] = sdist_table add_file_info(sdist_table, sdist, pool_info[sdist["file"]]) - if wheels := list(artifacts.get("wheel", [])): + if wheels := [ + f for f in package.files if f["file"].endswith(".whl") + ]: wheel_array = array() data["wheels"] = wheel_array wheel_array.multiline(True) diff --git a/tests/test_exporter_pylock_toml.py b/tests/test_exporter_pylock_toml.py index 4721ca3..4b078a4 100644 --- a/tests/test_exporter_pylock_toml.py +++ b/tests/test_exporter_pylock_toml.py @@ -833,6 +833,88 @@ def test_export_pypi_dependencies_multiple_wheels( assert content == expected +def test_export_pypi_dependencies_sdist_sorted_between_wheels( + tmp_path: Path, poetry: Poetry +) -> None: + # The lock file sorts files by name, so the sdist can end up between wheels, + # e.g. if the distribution name of some wheels is normalized differently. + lock_data = { + "package": [ + { + "name": "foo.bar", + "version": "1.0", + "optional": False, + "python-versions": "*", + "groups": [MAIN_GROUP], + "files": [ + { + "file": "foo.bar-1.0-py2-none-any.whl", + "hash": "sha256:abcdef1234567891", + }, + { + "file": "foo.bar-1.0.tar.gz", + "hash": "sha256:0123456789abcdef", + }, + { + "file": "foo_bar-1.0-py3-none-any.whl", + "hash": "sha256:abcdef1234567890", + }, + ], + }, + ], + "metadata": {"lock-version": "2.1"}, + } + poetry.locker.mock_lock_data(lock_data) # type: ignore[attr-defined] + + foo_bar = Package("foo.bar", "1.0") + foo_bar.files = [ + { + "file": "foo.bar-1.0-py2-none-any.whl", + "hash": "sha256:abcdef1234567891", + "url": "https://example.org/foo.bar-1.0-py2-none-any.whl", + }, + { + "file": "foo.bar-1.0.tar.gz", + "hash": "sha256:0123456789abcdef", + "url": "https://example.org/foo.bar-1.0.tar.gz", + }, + { + "file": "foo_bar-1.0-py3-none-any.whl", + "hash": "sha256:abcdef1234567890", + "url": "https://example.org/foo_bar-1.0-py3-none-any.whl", + }, + ] + poetry.pool.repository("PyPI").add_package(foo_bar) + + exporter = Exporter(poetry, NullIO()) + + exporter.export("pylock.toml", tmp_path, "pylock.toml") + + with (tmp_path / "pylock.toml").open(encoding="utf-8") as f: + content = f.read() + + expected = """\ +lock-version = "1.0" +created-by = "poetry-plugin-export" + +[[packages]] +name = "foo-bar" +version = "1.0" +index = "https://pypi.org/simple" +sdist = {name = "foo.bar-1.0.tar.gz", url = "https://example.org/foo.bar-1.0.tar.gz", hashes = {sha256 = "0123456789abcdef"}} +wheels = [ + {name = "foo.bar-1.0-py2-none-any.whl", url = "https://example.org/foo.bar-1.0-py2-none-any.whl", hashes = {sha256 = "abcdef1234567891"}}, + {name = "foo_bar-1.0-py3-none-any.whl", url = "https://example.org/foo_bar-1.0-py3-none-any.whl", hashes = {sha256 = "abcdef1234567890"}}, +] + +[tool.poetry-plugin-export] +groups = ["main"] +extras = [] +""" + + assert content == expected + + def test_export_legacy_repo_dependencies(tmp_path: Path, poetry: Poetry) -> None: lock_data = { "package": [