fixtures: order fixture override chains by visibility#14566
Open
bluetech wants to merge 1 commit into
Open
Conversation
The override chain for a fixture name was determined by the semi-incidental order in which fixturedefs were registered, with an ad-hoc tweak that pushed fixturedefs with no location to the front of the list (the last one is used first). Order the fixturedef list by partial order based on visibility instead: a fixturedef whose visibility is more specific (a descendant in the collection tree) sorts after a more general (ancestor) one, so it takes precedence in the override chain. Fixturedefs with non-comparable visibility keep registration order (last registered wins). The idea is that a fixture that defined closer to the item should take precedence. This generalizes the previous "no location" handling and makes precedence robust for programmatically registered fixtures. The code is complicated by the need to support FixtureDefs with legacy string nodeids rather than Nodes visibility, but these are deprecated and will removed in pytest 10, then we can remove the compat code. Also, comparing Nodes by visibility is not a super fast operation, since we need to look at the actual tree. However, I think overrides are not widely used and when they do, the chains are not very long, so hopefully that's fine. But it can be optimized using some tricks if necessary. Fix #14513
This was referenced Jun 6, 2026
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.
This PR is stacked on:
get_scope_packagewhen possible #14565The override chain for a fixture name was determined by the semi-incidental order in which fixturedefs were registered, with an ad-hoc tweak that pushed fixturedefs with no location to the front of the list (the last one is used first).
Order the fixturedef list by partial order based on visibility instead: a fixturedef whose visibility is more specific (a descendant in the collection tree) sorts after a more general (ancestor) one, so it takes precedence in the override chain. Fixturedefs with non-comparable visibility keep registration order (last registered wins).
The idea is that a fixture that defined closer to the item should take precedence. This generalizes the previous "no location" handling and makes precedence robust for programmatically registered fixtures.
The code is complicated by the need to support FixtureDefs with legacy string nodeids rather than Nodes visibility, but these are deprecated and will removed in pytest 10, then we can remove the compat code.
Also, comparing Nodes by visibility is not a super fast operation, since we need to look at the actual tree. However, I think overrides are not widely used and when they do, the chains are not very long, so hopefully that's fine. But it can be optimized using some tricks if necessary.
Fix #14513