Skip to content

Move fixture cache from FixtureDef to SetupState#14770

Open
seifertm wants to merge 18 commits into
pytest-dev:mainfrom
seifertm:move_fixture_cache_from_fixturedef_to_setupstate
Open

Move fixture cache from FixtureDef to SetupState#14770
seifertm wants to merge 18 commits into
pytest-dev:mainfrom
seifertm:move_fixture_cache_from_fixturedef_to_setupstate

Conversation

@seifertm

@seifertm seifertm commented Jul 23, 2026

Copy link
Copy Markdown

This PR removes FixtureDef.cached_result in favor of a cache that is attached to SetupState. FixtureRequest received methods to manipulate the fixture cache.

The PR is easiest reviewed commit by commit.

Open issues

Previously, fixture values were cached as part of FixtureDef objects and the cache entries contained a cache_key. The cache key is either None for non-parametrized fixtures or SubRequest.param. This allows invalidating cache entries when fixture parameters change.

Hoisting up the fixture cache to SetupState technically makes FixtureDef part of the cache key. This means we should use a two-dimensional index (e.g. tuple[FixtureDef, param]) to store entries in the fixture cache. Unfortunately, this is tricky, because TopRequest doesn't know about the current fixture param and therefore doesn't know the cache key.

This work has been done as part of the pytest sprint 2026, sponsored by Omicron.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 23, 2026
@seifertm
seifertm force-pushed the move_fixture_cache_from_fixturedef_to_setupstate branch from c54967f to 6678e5d Compare July 23, 2026 10:19
@seifertm
seifertm marked this pull request as ready for review July 23, 2026 10:19
Comment thread src/_pytest/setuponly.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missed

@seifertm seifertm Jul 24, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good find, thanks @RonnyPfannschmidt !

I pushed 5 additional commits. The tracking of the currently active request parameter for --setup-show is no longer attached to FixtureDef. Instead, --setup-show uses the existing SetupState.fixture_cache, which already contains the active fixture param as a cache key. The variable naming was changed from "cache_key" to "param" or "active_param".

In order to distinguish between "no param" and "param=None", I introduced a sentinel value. This prevents --setup-show from printing my_fixture[None] for unparametrized fixtures.

@bluetech

Copy link
Copy Markdown
Member

Previous stalled PR you might want to use as a reference/comparison: #14104

@seifertm

Copy link
Copy Markdown
Author

Thanks @bluetech! This topic was raised during the pytest sprint and I wasn't aware that there have been previous attempts at this. I'll check out the PR.

seifertm added 18 commits July 24, 2026 11:34
…hing fixture results to distinguish between *no param* and *None*.
@seifertm
seifertm force-pushed the move_fixture_cache_from_fixturedef_to_setupstate branch from cf3854d to 61286bd Compare July 24, 2026 09:34
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.

3 participants