Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,11 @@ def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainR
reprtraceback: ReprTraceback | ReprTracebackNative
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_)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To improve clarity, let's rename excinfo_ to current_excinfo.

reprtraceback = ReprTracebackNative(
format_exception(
type(excinfo.value),
excinfo.value,
type(excinfo_.value),
excinfo_.value,
traceback[0]._rawentry if traceback else None,
)
)
Expand Down
32 changes: 32 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,3 +2009,35 @@ def test_check_error_notes_failure(
with pytest.raises(AssertionError):
with pytest.raises(type(error), match=match):
raise error


def test_chained_exceptiongroup_uses_current_excinfo(pytester: Pytester) -> None:
"""Regression test: when a BaseExceptionGroup appears as a chained exception,
repr_excinfo should use excinfo_ (current) not excinfo (original outer).

With the bug, the traceback and type info from the outer exception was used
instead of the chained ExceptionGroup, potentially causing wrong output or
a mismatch between the displayed exception type and its traceback.
"""
pytester.makepyfile(
"""
import sys
if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup

def test_chained_eg():
try:
raise ExceptionGroup("inner", [TypeError("from inner")])
except BaseException:
raise ValueError("outer")
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*ExceptionGroup*",
"*TypeError: from inner*",
"*During handling of the above exception, another exception occurred:*",
"*ValueError: outer*",
]
)