Skip to content

fixtures: a couple of bug fixes for pytest_fixture_post_finalizer#14275

Open
bluetech wants to merge 3 commits intopytest-dev:mainfrom
bluetech:fixturedef-finish-idempotent
Open

fixtures: a couple of bug fixes for pytest_fixture_post_finalizer#14275
bluetech wants to merge 3 commits intopytest-dev:mainfrom
bluetech:fixturedef-finish-idempotent

Conversation

@bluetech
Copy link
Member

@bluetech bluetech commented Mar 7, 2026

These are changes extracted from @Anton3's PR #14104. That PR is big, so I decided to split this part so we can have a focused discussion and decision on these particular changes, and then #14104's can be a bit smaller.

The first test only adds a failing (xfail) test for #5848, that is that pytest_fixture_post_finalizer is sometimes called twice for the same teardown. I think this is definitely undesirable. We could state that pytest_fixture_post_finalizer needs to be idempotent and be prepared to be called multiple times, but I don't see any reason for that. It's also somewhat wasteful.

The second commit is not from @Anton3's PR, but just fixes the above problem in the most direct way, namely to do an early exit if already finished (cached_result is None). There is a possible gotcha here is somehow addfinalizer is called after finish, but I'm pretty sure this can't happen. Would have added an assert but it's not easy I think. I think this is good change anyway to avoid uselessly running the finish code when it will have no effect (well, except the pytest_fixture_post_finalizer call).

The third commit is @Anton3's different clever fix for the problem and also for another issue #14114 that things aren't handled gracefully if pytest_fixture_post_finalizer raises. I'm not a 100% on this:

  1. I think the pytest_fixture_post_finalizer is not very realistic, generally if hooks raise it's not something we really try to handle, it's basically a broken plugin. I think this is also what @RonnyPfannschmidt mean in the issue.
  2. The solution of making it a finalizer is a bit of a semantic mixup.
    However, I think handling it is still nice, and the semantics can make sense from a certain perspective. So it has my approval..

Anton3 and others added 3 commits March 8, 2026 00:13
…le times

Fixes pytest-dev#5848.

Co-authored-by: Ran Benita <ran@unusedvar.com>
Previously, if `pytest_fixture_post_finalizer` raised an error, the
fixture was not reset properly, because the `FixtureDef.finish()` was
disrupted.

Fix this by making `pytest_fixture_post_finalizer` itself part of the
teardown as a finalizer, which already has proper error handling. If it
raises, it's handled (and shown to the user) the same as a teardown
failure.

Fix pytest-dev#14114.
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Mar 7, 2026
@Anton3
Copy link
Contributor

Anton3 commented Mar 10, 2026

LGTM, @RonnyPfannschmidt please review

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