Repository navigation
Stop raises context managers from keeping caught exceptions alive - #15481
Draft
RonnyPfannschmidt wants to merge 6 commits into
Draft
RonnyPfannschmidt wants to merge 6 commits into
RonnyPfannschmidt wants to merge 6 commits into
Conversation
The ExceptionInfo of an active ``with`` block now lives in a context variable instead of on the context manager, so a pytest.raises() object that outlives its block (e.g. a parametrize value held by the mark) no longer pins the traceback and the test frame's locals. This also makes nested, recursive and concurrent use of one object fill the right ExceptionInfo. The accidentally public ``excinfo`` attribute is deprecated (removal in pytest 10); it still returns the last ExceptionInfo, which pytest drops after the teardown of tests that receive the object as a parameter. Fixes #13410 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
Replace the module-wide tuple of (context manager, ExceptionInfo) pairs with one ContextVar per raises object, holding a linked chain of that object's active blocks. Exiting resets the token, so the variable leaves the context again after the outermost block. The variable is dropped on copy and pickle, which keeps raises objects copyable as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
A context copied inside a block (e.g. a task created there) or a block exited from another context can keep the block object referenced after the block is done. Empty it on exit and skip emptied blocks, so those references no longer pin the traceback and frame locals. Use one constant name for the per-object ContextVar. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
Exiting a block in a thread or asyncio task other than the one that entered it (e.g. an async generator whose setup and teardown run in different tasks) now uses that block when it is the object's only open block, and warns. It raises a RuntimeError that says why when the exit is ambiguous or has no matching enter, instead of an internal AssertionError. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
RonnyPfannschmidt
requested review from
bluetech and
nicoddemus
and removed request for
nicoddemus
November 28, 2026 11:33
5 tasks done
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.
Requested by Ronny · project thread
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
A
pytest.raises()object used as a parametrize value kept its last caught exception, traceback and the test frame's locals alive for the whole session. Reusing one object for nested, recursive or concurrent blocks filled the wrongExceptionInfoor crashed. Each raises object now tracks its active blocks in its own context variable. The accidentally publicexcinfoattribute is deprecated: it still works with a warning, and pytest clears it after teardown for parametrized tests. The raises context managers also supportasync withnow. Tests cover the leak, every reuse case,async withand the deprecation.Fixes #13410
🤖 Generated with Claude Code
https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9