Skip to content

Refactor fixture addfinalizer to utilize ExitStack (#5592) - #15208

Closed
giovanamalaia wants to merge 5 commits into
pytest-dev:mainfrom
giovanamalaia:refactor-exit-stack-5592
Closed

giovanamalaia wants to merge 5 commits into
pytest-dev:mainfrom
giovanamalaia:refactor-exit-stack-5592

Conversation

@giovanamalaia

Copy link
Copy Markdown

Summary
This PR refactors the internal fixture finalizer flow to use contextlib.ExitStack.

What I changed

  • Replaced the internal fixture finalizer storage with ExitStack.
  • Kept the same teardown behavior:
    • finalizers still run in reverse order (LIFO)
    • all finalizers still run even if one fails
    • teardown errors are still grouped/re-raised as before
  • Added a regression test to ensure finalizers from one test do not leak into the next test.

Validation
Ran focused fixture/finalizer tests locally, including addfinalizer and post-finalizer scenarios.

AI Assistance

  • GitHub Copilot was used as an assistant during the testing phase, with all changes thoroughly reviewed, tested, and validated locally by the author.

Closes #5592

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 18, 2026
@RonnyPfannschmidt

RonnyPfannschmidt commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Rejected with prejudice

Putting exitstack thete but then sidestepping it is worse than what we have in place

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.

Investigate replacing addfinalizer with ExitStack

2 participants