Fix rootdir conftest fixtures not visible to items collected outside the rootdir#14694
Open
breidenbach0 wants to merge 1 commit into
Open
Conversation
…#14683) A conftest located in the rootdir used to get an empty baseid (matching every collected item), so its fixtures were visible session-wide. The node-based scoping introduced in 46478fa (pytest-dev#14098, pytest 9.1) scoped it to its own Directory node instead. When --rootdir points to a subdirectory and tests/doctests are collected from a parent directory, that Directory is not an ancestor of the collected items, so the conftest fixtures -- including doctest_namespace injections -- became invisible (NameError in doctests). Restore the pre-9.1 behavior by attaching the rootdir conftest to the Session node, in both places a conftest is parsed: the initial-conftest flush and the Directory collect report. This is behavior-identical for normal layouts (the Session is an ancestor of all items under the rootdir), preserves the pytest-dev#14004 sibling-leak fix, and keeps fixture overrides working (a subdir conftest is still more specific than the Session).
Author
|
Heads up on the two red CI jobs — both are a known repo-wide infra issue, unrelated to this change:
For confirmation, another currently-open PR (#14693, unrelated) fails the identical two jobs with the same cause. All other jobs on this PR (macOS/Ubuntu, Python 3.10–3.15, xdist, plugins, doctesting, etc.) are green. |
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.
Problem
Fixes #14683.
doctest_namespaceinjections (and any fixture defined in the rootdir conftest) stopped being available in pytest 9.1 when--rootdirpoints to a subdirectory and tests/doctests are collected from a parent directory. Doctests then fail withNameError, e.g. the reporter's invocation:Reproduced: 9.0.3 passes, 9.1.1 fails with
NameError: name '...' is not defined.Cause
Bisected to
46478fad5(#14098, pytest 9.1), which changed conftest fixture visibility from nodeid-based to node-based:baseid(relative to rootdir), which matches every collected item — its fixtures were visible session-wide.Directorynode. When items are collected from outside the rootdir, thatDirectoryis a sibling subtree, not an ancestor of the items, so the fixtures are no longer visible.Fix
Restore the pre-9.1 behavior by attaching the rootdir conftest to the
Sessionnode, in the two places a conftest gets parsed:_flush_pending_conftests_to_session— also flush the initial rootdir conftest (e.g. when it is the--config-file) to theSession.pytest_make_collect_report— when collecting the rootdirDirectory, attach its conftest to theSessioninstead of theDirectory.This is safe:
Sessionis an ancestor of all items, so visibility is unchanged.Directory, so sibling-leak does not return.Sessionperis_visibility_more_specific, so it still wins.Tests
testing/test_conftest.py::test_rootdir_conftest_visible_outside_rootdirtesting/test_doctest.py::TestDoctestNamespaceFixture::test_namespace_fixture_from_rootdir_when_modules_outside_rootdir(mirrors the reporter's exact--config-file+ collect-from-parent pattern)Both fail on
mainand pass with this change. Added news fragmentchangelog/14683.bugfix.rst.Note on #14635
#14635 (Home Assistant, "fixtures not found when a parent directory appears multiple times in an argument list") is tracked as a duplicate and is being worked on separately by @RonnyPfannschmidt (multiple
Directorynodes for the same path). This PR targets the rootdir-conftest visibility facet (#14683) only; making the rootdir conftestSession-visible also makes it more robust against the multi-node scenario, but it is not meant to be the complete fix for #14635.