Skip to content

Add pytest.register_fixture#14568

Open
bluetech wants to merge 5 commits into
more-visibility-deprecatefrom
register-fixture
Open

Add pytest.register_fixture#14568
bluetech wants to merge 5 commits into
more-visibility-deprecatefrom
register-fixture

Conversation

@bluetech
Copy link
Copy Markdown
Member

@bluetech bluetech commented Jun 6, 2026

This PR is stacked on:

Expose FixtureManager._register_fixture as a stable public pytest.register_fixture interface, for advanced use by plugins (also replaces internal uses in the python and unittest plugins).

Fix #12376.

bluetech added 5 commits June 6, 2026 20:56
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
has_location was used to determine the fixture override order, pushing
fixturedefs with no location to the front of the override chain. Now
that the override order is determined by the fixtures' visibility in the
collection tree, the attribute is no longer needed.

Turn it into a property that emits a PytestRemovedIn10Warning, backed by
the private _has_location attribute, and document the deprecation.
…ility instead of None

Let's get rid of the `None` global visibility. Use `Session` which is
functionality equivalent instead.

- Allows us to remove an annoying special case from the fixture core.
- Allows a cleaner public `register_fixture` interface (upcoming).

There was previously a distinction between "has location" (e.g. conftest
plugins) and "no location" (e.g. external plugins), but it's no longer
needed after recent changes. Since initial conftest plugins are always
registered after core & external plugins, the override chain order is
maintained.
The nodeid-visibility/baseid deprecation previously allowed None
(global) visibility. But this visibility is no longer needed, should use
Session visibility instead. So let's include it in the deprecation as
well, this way we can have a clean break in pytest 10.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 6, 2026
@bluetech bluetech force-pushed the more-visibility-deprecate branch from b50559e to 547eb13 Compare June 7, 2026 18:52
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.

1 participant