Skip to content

fix: deduplicate Directory nodes on re-collection to preserve fixture identity (#14635)#14645

Draft
RonnyPfannschmidt wants to merge 1 commit into
pytest-dev:mainfrom
RonnyPfannschmidt:fix/14635-deduplicate-directory-nodes-on-recollection
Draft

fix: deduplicate Directory nodes on re-collection to preserve fixture identity (#14635)#14645
RonnyPfannschmidt wants to merge 1 commit into
pytest-dev:mainfrom
RonnyPfannschmidt:fix/14635-deduplicate-directory-nodes-on-recollection

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Summary

Fixes #14635.

When Session re-collects a parent Directory (due to handle_dupes=False for file CLI args), fresh child nodes were created. Since fixture registration uses node identity for matching (_matchfactories checks fixturedef.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_node with handle_dupes=False calls collect() on the parent Directory, which creates brand-new child Directory/Package instances. The _collection_cache is 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-duplicates semantics (which needs fresh test items).

Test plan

Made with Cursor

… 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error collecting tests on 9.1.1 - fixtures not found when a parent directory appears multiple times in a argument list

1 participant