fix: deduplicate Directory nodes on re-collection to preserve fixture identity (#14635)#14645
Draft
RonnyPfannschmidt wants to merge 1 commit 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>
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