Skip to content

Do not register function-scoped fixture finalizers with requested fixtures - #15224

Open
rafael-g-v wants to merge 1 commit into
pytest-dev:mainfrom
rafael-g-v:fix-4871
Open

rafael-g-v wants to merge 1 commit into
pytest-dev:mainfrom
rafael-g-v:fix-4871

Conversation

@rafael-g-v

Copy link
Copy Markdown

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.scope check breaks a class-scoped fixture
that 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.

  • Added a test that checks finalizers don't accumulate (fails on main).
  • Added a test for the class/module parametrization case (fails with the scope == version).
  • Added a changelog entry.

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!

…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
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 19, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

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.

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

No deployments
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.

2 participants