Repository navigation
Raise a clear error when xdist is enabled after pytest-cov has started - #749
Open
pctablet505 wants to merge 2 commits into
Open
pctablet505 wants to merge 2 commits into
pctablet505 wants to merge 2 commits into
Conversation
yangfan-yf-yf
approved these changes
Jul 25, 2026
yangfan-yf-yf
left a comment
There was a problem hiding this comment.
Reviewed the late-xdist path on 98cc2f6.
python -m pytest tests/test_pytest_cov.py::test_xdist_enabled_after_initial_conftests -qpassed on Windows/Python 3.12.- Ruff linting and formatting checks pass for the changed plugin and test files, and
git diff --checkis clean.
The guard is limited to the two distributed-only hooks, preserves the already-started coverage controller, and turns the previously uncaught missing-method path into the project’s existing DistCovError with actionable configuration guidance. The changelog and contributor entries follow the repository contribution checklist.
pctablet505
marked this pull request as ready for review
July 27, 2026 10:42
If another plugin enables pytest-xdist only after pytest-cov's own pytest_load_initial_conftests has already run (for example a plugin that reacts to addopts from a config file), pytest-cov had already committed to centralised coverage measurement. When xdist then called pytest_configure_node, this crashed with a cryptic "AttributeError: 'Central' object has no attribute 'configure_node'". Detect this mismatch and raise the existing DistCovError with an actionable message instead. Fixes pytest-dev#740
pctablet505
force-pushed
the
fix-xdist-configure-node-crash-740
branch
from
September 28, 2026 09:25
98cc2f6 to
b334857
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #740
If a plugin turns on pytest-xdist after pytest-cov has started, pytest-cov crashes with
INTERNALERROR> AttributeError: 'Central' object has no attribute 'configure_node'. In #740 the plugin is pytest-enabler, in setuptools' test setup. With this change pytest-cov raises aDistCovErrorinstead, which explains the cause and the config change that avoids it.On scope: the reporter's setup still won't run. It fails with a clear message instead of an internal error. Making that case actually work is a bigger change, covered under Notes.
Problem
pytest-cov picks its controller in its
tryfirstpytest_load_initial_conftests, based on the xdist options visible at that moment. If-n(or--distwith--tx) is present, it usesDistMaster, otherwiseCentral. It has to start that early so that modules imported while conftests load are measured. When another plugin adds-nafter that, xdist starts workers and callspytest_configure_nodeon aCentralcontroller, which doesn't have that method.Fix
pytest_configure_nodeandpytest_testnodedownnow check that the controller is aDistMaster, and raiseDistCovErrorif it isn't. The error message says to make-n/--distvisible before pytest-cov starts: put them on the command line, or in theaddoptsthat pytest reads itself rather than ones a plugin injects later. Runs where xdist is configured normally don't change. CHANGELOG and AUTHORS are updated.Tests
test_xdist_enabled_after_initial_conftestsloads a plugin with-p. That plugin'spytest_load_initial_conftestsappends-n 2after pytest-cov's hook has run. The test asserts that the output containspytest_cov.DistCovError: pytest-xdist workers were startedand does not containAttributeError. On master it fails with the originalAttributeError. The rest oftests/test_pytest_cov.pypassed locally, excepttest_celery, which fails the same way on master because Docker/testcontainersisn't available in my environment.Notes
The other way to fix this is to support the late case: the first time
pytest_configure_nodefires on aCentralcontroller, promote it toDistMasterin place. That would make the #740 setup work. It is a behaviour change in the engine, though, and would need tests for both collocated and remote workers, so this PR stops at the error. Do you want that instead of (or after) this? If you'd rather keep #740 open for that, I'll changeFixestoRefs.