Repository navigation
Description
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest-xdist maintainers; I prompted it, it did the work, I read it.
Follow-up to #500, rescoped for the current code. That PR is being closed because it can't be rebased: it targeted xdist 1.31 and bundled a rewrite of every load scheduler. Most of that rewrite has landed in other forms since then. Background: #18.
Current state
LoadFileSchedulingandLoadGroupSchedulingsubclassLoadScopeSchedulingand only override_split_scope(nodeid), so the "one base class, override the grouping" part of Custom test grouping and test group order logic #500 already exists.--dist loadgroup/@pytest.mark.xdist_group(Create new dist option 'loadgroup' #733) covers marker-based grouping, but it smuggles the group to the controller as an@groupnodeid suffix. Loadgroup: Don't modify test nodeids any longer. #1119 proposes an explicit side-channel for that.--loadscope-reorder(on by default) sorts groups by size inLoadScopeScheduling.schedule().pytest_xdist_make_schedulerstill lets a plugin replace the whole scheduler. That is heavy when all you want is different grouping.
Proposal
- Grouping hook, consulted by
LoadScopeScheduling._split_scopebefore the subclass default:Call it once per nodeid when the workqueue is built, and cache the result. It must not be re-invoked from@pytest.hookspec(firstresult=True) def pytest_xdist_get_test_group(config: pytest.Config, nodeid: str) -> str | None: """Return the work-unit key for ``nodeid``, or None to use the scheduler default."""
mark_test_complete. - Ordering hook, called after the workqueue is built in
schedule():This needs a defined precedence relative to@pytest.hookspec(firstresult=True) def pytest_xdist_order_test_groups( config: pytest.Config, groups: dict[str, list[str]] ) -> list[str] | None: """Return group keys in dispatch order, or None to keep the default."""
--loadscope-reorder. Suggestion: a non-None result wins, and reorder only applies when no hook answers. - Out of scope:
--dist loadkeeps its per-test batching. Custom test grouping and test group order logic #500 turnedloadinto one test per group, which removes batching and costs more round-trips. Users who want grouping pickloadscopeplus the hook.
Known limitation / open question
The controller only has nodeid strings, so the hook can't see marks, fixtures or fixture-scope instances. That was #18's actual goal (phases 2 and 3). A fully useful version needs workers to send per-item metadata with collectionfinish, which is the same channel #1119 needs. Should that land first, so the hook gets (nodeid, metadata) from the start instead of growing a second signature later?
Notes for an implementer
- Don't port Custom test grouping and test group order logic #500's scheduler bugs:
loadscopemust keeprsplit("::", 1). _assign_work_unitusesworker_collection.index(nodeid)per test, which is O(N) per lookup. With fine-grained custom groups this becomes quadratic, so a nodeid-to-index map should come with it.- Tests: grouping hook returning
Nonevs. a key; ordering hook vs.--loadscope-reorder; crash/replace-worker path with custom groups (see the 2026-04 loadgroup hang fix).
Activity
sahilmathur254 commentedon Oct 21, 2026
Hi! I’m interested in working on this as a focused contribution.
Looking at current master, #1388 already transports group names alongside collected node IDs, and #1119 was closed as superseded. This doesn’t yet provide general mark or fixture metadata.
Would you prefer starting with the node-ID-based grouping hook proposed here, or should its initial API expose worker-provided metadata?
My proposed scope would be:
I’d leave --dist load batching unchanged. I also noticed #1383 overlaps the collection-index optimization, so I’d coordinate with that work rather than duplicate it.
Does that scope and API direction sound appropriate?
RonnyPfannschmidt commentedon Nov 27, 2026
@sahilmathur254 im currently swamped nd cant provide good guidance - this is a delciate topic so coordination with the maintainers will be necessary for success
currently i'd recommend smaller size for the initial attempts simply because pytest-core is very overloaded thanks to the AI-bros running a DOS attack on opensource volunteer maintenance