Repository navigation
Do not register function-scoped fixture finalizers with requested fixtures - #15224
Open
rafael-g-v wants to merge 1 commit into
Open
rafael-g-v wants to merge 1 commit into
rafael-g-v wants to merge 1 commit into
Conversation
…tures Function-scoped fixtures are always torn down by SetupState at the end of each test, before any higher-scoped fixture can be torn down or invalidated. Registering their finalizer with the fixtures they request was therefore unnecessary, and it made finalizers accumulate on higher-scoped fixtures (for example, one per test on tmp_path_factory for every test using tmp_path). Fixtures with a wider scope still register their finalizer, since a class-scoped fixture that requests a parametrized module-scoped fixture must be invalidated when the parameter changes while the class is still active. Add a test for that case as well. Refs pytest-dev#4871
RonnyPfannschmidt
approved these changes
Oct 19, 2026
RonnyPfannschmidt
left a comment
Member
There was a problem hiding this comment.
indeed, its not the final change we want for this but it certainly removes pressure for finalizers
#14104 is a more complete solution that needs iteration as well
This branch has not been deployed
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.
Refs #4871
This does not implement the full refactoring discussed in #4871, only a smaller
improvement along the lines of the "In the meantime, anything we can do?" comment there.
Function-scoped fixtures no longer register their finalizer with the fixtures they
request. They are always torn down by SetupState at the end of each test, so the
registration was unnecessary, and it made finalizers accumulate on higher-scoped
fixtures: with 1000 tests using tmp_path, tmp_path_factory ended up with 1000 finalizers.
The suggested
fixturedef.scope == request.scopecheck breaks a class-scoped fixturethat requests a parametrized module-scoped fixture: the class fixture keeps the old
value after the parameter changes. No existing test caught this, so I added a test
for it. That's why this change only applies to function-scoped fixtures.
scope ==version).I'm a student; I used an AI assistant to help with this change, and I reviewed and
tested it myself. Any feedback is welcome!