fix: deduplicate Directory nodes on re-collection to preserve fixture identity (#14635)#14645
Open
RonnyPfannschmidt wants to merge 2 commits into
Conversation
… identity (pytest-dev#14635) When Session re-collects a parent Directory (handle_dupes=False for file CLI args), fresh child nodes were created. Since fixture registration uses node identity for matching, the new Directory children didn't match fixtures registered with the original instances. Reuse previously-seen Directory children (by path) when re-collecting a parent. Module/File nodes are still recreated to preserve --keep-duplicates semantics. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
441d49f to
ecdc8d5
Compare
…#14635 Add a changelog entry explaining the fix for fixture resolution failures when multiple CLI paths share ancestor directories, and a comment on the dedup code explaining why post-processing was chosen over a session-global deduplication mechanism. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
2724383 to
db00165
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a regression where collecting multiple CLI paths (mixing directories and files) could cause a parent directory to be re-collected and its child Directory nodes to be replaced with fresh instances, breaking fixture visibility because fixture registration relies on collector node identity.
Changes:
- Reconciles re-collected
Directorychildren with previously-seen instances whenhandle_dupes=False, preserving fixture identity while still recreating file/module nodes for--keep-duplicatessemantics. - Adds a regression test covering order-dependent fixture-closure/parametrize validation failures triggered by mixed dir/file collection arguments.
- Adds a changelog entry describing the fixed behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/_pytest/main.py |
Reuses cached Directory children after re-collection to keep fixture registration tied to stable node identities. |
testing/test_conftest.py |
Adds a regression test that fails on order-dependent collection when directory nodes are recreated. |
changelog/14635.bugfix.rst |
Documents the fixture-resolution regression and its order-dependent symptom. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Fixes #14635.
When
Sessionre-collects a parent Directory (due tohandle_dupes=Falsefor file CLI args), fresh child nodes were created. Since fixture registration uses node identity for matching (_matchfactorieschecksfixturedef.node in parent_nodes), the new Directory children didn't match fixtures registered with the original instances.This caused fixture closure computation to fail for tests collected after unrelated paths, because the conftest fixtures were registered with a now-orphaned Directory node instance.
Root cause:
_collect_one_nodewithhandle_dupes=Falsecallscollect()on the parent Directory, which creates brand-new child Directory/Package instances. The_collection_cacheis then updated with these new children. Later collection paths that look up cached children find different node objects than the ones conftest fixtures were registered with.Fix: After re-collection, replace any freshly-created Directory children with the previously-seen instances for the same path. Module/File nodes are still recreated to preserve
--keep-duplicatessemantics (which needs fresh test items).Test plan
test_fixture_closure_order_independence_with_parametrizethat reproduces the exact scenario from Error collecting tests on 9.1.1 - fixtures not found when a parent directory appears multiple times in a argument list #14635TestOverlappingCollectionArguments::test_complex_combined_handling(which exercises--keep-duplicateswith overlapping file/dir args) passes--keep-duplicates file.py file.pystill collects the file twiceMade with Cursor