Skip to content

fixtures: order fixture override chains by visibility#14566

Merged
bluetech merged 1 commit into
mainfrom
visibility-override
Jun 7, 2026
Merged

fixtures: order fixture override chains by visibility#14566
bluetech merged 1 commit into
mainfrom
visibility-override

Conversation

@bluetech
Copy link
Copy Markdown
Member

@bluetech bluetech commented Jun 6, 2026

This PR is stacked on:

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

Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one is gnarly - good fix

Base automatically changed from getscopepkg-node to main June 7, 2026 15:15
@bluetech bluetech force-pushed the visibility-override branch from 09399fe to 41bc25f Compare June 7, 2026 15:16
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 imperatively 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
@bluetech bluetech force-pushed the visibility-override branch from 41bc25f to 7186cd4 Compare June 7, 2026 18:32
@bluetech bluetech merged commit e0fab11 into main Jun 7, 2026
36 checks passed
@bluetech bluetech deleted the visibility-override branch June 7, 2026 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixture override order should be determined by visibility?

2 participants