fix: deduplicate Directory nodes on re-collection to preserve fixture identity (#14635) - #14645
Conversation
441d49f to
ecdc8d5
Compare
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.
… 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>
…#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>
db00165 to
b054cac
Compare
Backport to 9.1.x: 💚 backport PR created✅ Backport PR branch: Backported as #14968 🤖 @patchback |
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