From 5b40039efa3d6e5bb034f050a79d5f969f588878 Mon Sep 17 00:00:00 2001 From: Brad Edwards Date: Thu, 1 Oct 2026 04:39:38 +0200 Subject: [PATCH] Fix SonarCloud finding: partition shard items with comprehensions --- tools/pytest_shard_plugin.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/pytest_shard_plugin.py b/tools/pytest_shard_plugin.py index 75da62bc6..58c4db7f0 100644 --- a/tools/pytest_shard_plugin.py +++ b/tools/pytest_shard_plugin.py @@ -65,10 +65,9 @@ def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item raise pytest.UsageError("--shard-count and --shard-index must be supplied together") index, count = validate_shard_index(shard_index, shard_count) - owned: list[pytest.Item] = [] - deselected: list[pytest.Item] = [] - for item in items: - (owned if shard_for_nodeid(item.nodeid, count) == index else deselected).append(item) + assignments = [(item, shard_for_nodeid(item.nodeid, count) == index) for item in items] + owned = [item for item, is_owned in assignments if is_owned] + deselected = [item for item, is_owned in assignments if not is_owned] if deselected: config.hook.pytest_deselected(items=deselected) items[:] = owned