code: use current excinfo when formatting chained BaseExceptionGroup#14221
Open
bysiber wants to merge 2 commits intopytest-dev:mainfrom
Open
code: use current excinfo when formatting chained BaseExceptionGroup#14221bysiber wants to merge 2 commits intopytest-dev:mainfrom
bysiber wants to merge 2 commits intopytest-dev:mainfrom
Conversation
In repr_excinfo, the BaseExceptionGroup branch incorrectly uses the original excinfo instead of excinfo_ (which tracks the current exception in the chain). This means that when walking __cause__ or __context__ chains, filter_excinfo_traceback and format_exception always operate on the first exception rather than the current one. The non-group branch on the else side already uses excinfo_ correctly.
Member
|
Similarly to your other PRs I commented on, could you please add a regression test to catch this issue? |
Test that when a BaseExceptionGroup appears as a chained exception, the traceback output correctly shows both the ExceptionGroup and the outer exception, using the current excinfo_ rather than the original.
Author
|
Added a regression test — it creates a chained exception where a BaseExceptionGroup is the context of an outer ValueError, and verifies the output correctly shows both exceptions with the right traceback chain. |
nicoddemus
reviewed
Feb 21, 2026
| if isinstance(e, BaseExceptionGroup): | ||
| # don't filter any sub-exceptions since they shouldn't have any internal frames | ||
| traceback = filter_excinfo_traceback(self.tbfilter, excinfo) | ||
| traceback = filter_excinfo_traceback(self.tbfilter, excinfo_) |
Member
There was a problem hiding this comment.
To improve clarity, let's rename excinfo_ to current_excinfo.
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.
In
repr_excinfo, when the current exceptioneis aBaseExceptionGroup, the code uses the originalexcinfoparameter instead ofexcinfo_(which tracks the current exception as the method walks the__cause__/__context__chain):On the first loop iteration this doesn't matter since
excinfo_starts asexcinfo. But when walking the exception chain,excinfo_is reassigned toExceptionInfo.from_exception(e)whileexcinfostill points to the original. So for any chainedBaseExceptionGroup, the traceback filtering is applied to the wrong exception andformat_exceptionformats the original exception instead of the current one.The non-group branch already correctly uses
excinfo_:This fix replaces the three occurrences of
excinfowithexcinfo_inside theBaseExceptionGroupbranch.