diff --git a/changelog/9159.bugfix.rst b/changelog/9159.bugfix.rst new file mode 100644 index 000000000..5e7f0a301 --- /dev/null +++ b/changelog/9159.bugfix.rst @@ -0,0 +1 @@ +Showing inner exceptions by forcing native display in ``ExceptionGroups`` even when using display options other than ``--tb=native``. A temporary step before full implementation of pytest-native display for inner exceptions in ``ExceptionGroups``. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 304a5cbd7..b1991f1aa 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -56,6 +56,18 @@ if TYPE_CHECKING: _TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"] +ExceptionGroupTypes: tuple = () # type: ignore +try: + ExceptionGroupTypes += (ExceptionGroup,) # type: ignore +except NameError: + pass # Is missing for `python<3.10` +try: + import exceptiongroup + + ExceptionGroupTypes += (exceptiongroup.ExceptionGroup,) +except ModuleNotFoundError: + pass # No backport is installed + class Code: """Wrapper around Python code objects.""" @@ -923,11 +935,20 @@ class FormattedExcinfo: seen: Set[int] = set() while e is not None and id(e) not in seen: seen.add(id(e)) - if excinfo_: - reprtraceback = self.repr_traceback(excinfo_) - reprcrash: Optional[ReprFileLocation] = ( - excinfo_._getreprcrash() if self.style != "value" else None + if isinstance(e, ExceptionGroupTypes): + reprtraceback: Union[ + ReprTracebackNative, ReprTraceback + ] = ReprTracebackNative( + traceback.format_exception( + type(excinfo.value), + excinfo.value, + excinfo.traceback[0]._rawentry, + ) ) + reprcrash: Optional[ReprFileLocation] = None + elif excinfo_: + reprtraceback = self.repr_traceback(excinfo_) + reprcrash = excinfo_._getreprcrash() if self.style != "value" else None else: # Fallback to native repr if the exception doesn't have a traceback: # ExceptionInfo objects require a full traceback to work. diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index af72857f3..78f777718 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -1470,3 +1470,34 @@ def test_no_recursion_index_on_recursion_error(): with pytest.raises(RuntimeError) as excinfo: RecursionDepthError().trigger assert "maximum recursion" in str(excinfo.getrepr()) + + +def test_exceptiongroup(pytester: Pytester) -> None: + pytester.makepyfile( + """ + def f(): raise ValueError("From f()") + def g(): raise RuntimeError("From g()") + + def main(): + excs = [] + for callback in [f, g]: + try: + callback() + except Exception as err: + excs.append(err) + if excs: + raise ExceptionGroup("Oops", excs) + + def test(): + main() + """ + ) + result = pytester.runpytest() + assert result.ret != 0 + + match = [ + r" | ExceptionGroup: Oops (2 sub-exceptions)", + r" | ValueError: From f()", + r" | RuntimeError: From g()", + ] + result.stdout.re_match_lines(match) diff --git a/tox.ini b/tox.ini index 93c390ffc..fd2ff1ba1 100644 --- a/tox.ini +++ b/tox.ini @@ -46,6 +46,7 @@ setenv = extras = testing deps = doctesting: PyYAML + exceptiongroup: exceptiongroup>=1.0.0 numpy: numpy>=1.19.4 pexpect: pexpect>=4.8.0 pluggymain: pluggy @ git+https://github.com/pytest-dev/pluggy.git