Backtestspytestrun-20261106-234554#12175

Issue #12175 Merged fix PR # Run 6 Nov 2026 · run-20261106-234554 pytest-dev/pytest

ExceptionInfo.for_later() wont add assertion strip text

Worse

The merged fix was judged better than our patch.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches correctly fix the reported path (pytest.raises -> for_later()/fill_unfilled() never set _striptext) and both add tests that fail before and pass after, covering the rewritten-assert case, the explicit AssertionError('message') case (unchanged, which matches the docstring's intent), and the unittest subTest crash line. The independent tests agree: both pass the failed-assert case and both leave the explicit-message case alone.

Patch A fixes it per construction site: it recomputes striptext in fill_unfilled() and separately swaps the unittest.py direct constructor call to from_exc_info(). That works, but it only fixes the sites it touches; any other place that builds an ExceptionInfo directly (or a future one) keeps the empty striptext default and reproduces the bug.

Patch B moves the computation to the point of use (_get_striptext(), lazily derived from self.value when _striptext is None), so every ExceptionInfo regardless of how it was built, including the untouched unittest.py call and from_exception, behaves consistently. It preserves the existing 'explicit exprinfo disables stripping' semantics, keeps the diff smaller, and its test additionally checks the from_exception path. The lazy cache mutating _striptext is a minor stylistic quirk but clearly commented. Scope is equivalent; B addresses the root cause more fully, so B is the better fix.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

6m 42s

Wall-clock time in the container

Full test suite

No new failures

414 passed · 13 skipped · 427 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

85vs 78 merged

Changed lines, ours vs merged

Using exc_info.exconly(tryshort=True) is meant to remove AssertionError: from the exception message according to (ref)

When 'tryshort' resolves to True, and the exception is an AssertionError, only the actual exception part of the exception representation is returned (so 'AssertionError: ' is removed from the beginning).

However, this is not the case. In addition, the tests do not assert this behaviour:

https://github.com/pytest-dev/pytest/blob/44895289c725ed922889c882b7f1b78b741eba20/testing/code/test_excinfo.py#L332-L339

MWE

cat <<-EOF > excinfo_exconly.py
import pytest

def test():
    with pytest.raises(AssertionError) as exc_info:
        raise AssertionError('message')

    assert exc_info.exconly(tryshort=True) == 'message'
EOF
pytest excinfo_exconly.py

Environment

Platform:              linux; (Linux-5.14.21-150500.55.52-default-x86_64-with-glibc2.31)
Python version:        3.10.13 (main, Sep 05 2023, 11:46:10) [GCC])
Python implementation: CPython
Pytest version:		   8.0.2

The two patches

Our patch+78 −7
changelog/12175.bugfix.rstnew file mode 100644index 000000000..d7aaea0a3@@ -0,0 +1 @@+Fixed :meth:`ExceptionInfo.exconly(tryshort=True) <pytest.ExceptionInfo.exconly>` not stripping ``AssertionError: `` for failed ``assert`` statements when the :class:`~pytest.ExceptionInfo` came from :func:`pytest.raises` or a failing ``unittest.TestCase.subTest``.src/_pytest/_code/code.pyindex e7712c48b..ae43d810f 100644@@ -557,15 +557,26 @@ class ExceptionInfo(Generic[E]):         exprinfo: str | None = None,     ) -> ExceptionInfo[E]:         """Like :func:`from_exception`, but using old-style exc_info tuple."""-        _striptext = ""+        _striptext = cls._compute_striptext(exc_info, exprinfo)+        return cls(exc_info, _striptext, _ispytest=True)++    @classmethod+    def _compute_striptext(+        cls,+        exc_info: tuple[type[BaseException], BaseException, TracebackType],+        exprinfo: str | None = None,+    ) -> str:+        """Return the prefix :meth:`exconly` strips when ``tryshort`` is true.++        Only failed ``assert`` statements get ``"AssertionError: "`` stripped.+        """         if exprinfo is None and isinstance(exc_info[1], AssertionError):             exprinfo = getattr(exc_info[1], "msg", None)             if exprinfo is None:                 exprinfo = saferepr(exc_info[1])             if exprinfo and exprinfo.startswith(cls._assert_start_repr):-                _striptext = "AssertionError: "--        return cls(exc_info, _striptext, _ispytest=True)+                return "AssertionError: "+        return ""      @classmethod     def from_current(cls, exprinfo: str | None = None) -> ExceptionInfo[BaseException]:@@ -596,6 +607,7 @@ class ExceptionInfo(Generic[E]):         """Fill an unfilled ExceptionInfo created with ``for_later()``."""         assert self._excinfo is None, "ExceptionInfo was already filled"         self._excinfo = exc_info+        self._striptext = self._compute_striptext(exc_info)      @property     def type(self) -> type[E]:@@ -653,8 +665,8 @@ class ExceptionInfo(Generic[E]):         information for SyntaxError's.          :param tryshort:-            If true, and the exception is an AssertionError, strip-            'AssertionError: ' from the beginning.+            If true, and the exception is an AssertionError raised by a failing+            ``assert`` statement, strip 'AssertionError: ' from the beginning.         """          def _get_single_subexc(src/_pytest/unittest.pyindex 2c9e0dcb8..f0ead1e8d 100644@@ -435,7 +435,7 @@ class TestCaseFunction(Function):         exception_info: ExceptionInfo[BaseException] | None         match exc_info:             case tuple():-                exception_info = ExceptionInfo(exc_info, _ispytest=True)+                exception_info = ExceptionInfo.from_exc_info(exc_info)             case ExceptionInfo() | None:                 exception_info = exc_info             case unreachable:testing/code/test_excinfo.pyindex ec9f584df..5304fa0d2 100644@@ -351,6 +351,39 @@ def test_excinfo_exconly():     assert msg.endswith("world")  +def test_excinfo_exconly_tryshort_strips_assertion_error() -> None:+    """ExceptionInfo filled by pytest.raises() (via for_later()/fill_unfilled())+    strips 'AssertionError: ' for failed assert statements, like from_exc_info()."""+    x = 1+    with pytest.raises(AssertionError) as excinfo:+        assert x == 2+    assert excinfo.exconly().startswith("AssertionError: assert 1 == 2")+    assert excinfo.exconly(tryshort=True).startswith("assert 1 == 2")+    reprcrash = excinfo._getreprcrash()+    assert reprcrash is not None+    assert reprcrash.message.startswith("assert 1 == 2")++    # Not a failed assert statement: nothing is stripped.+    with pytest.raises(AssertionError) as excinfo:+        raise AssertionError("message")+    assert excinfo.exconly(tryshort=True) == "AssertionError: message"+++def test_excinfo_for_later_fill_unfilled_striptext() -> None:+    try:+        raise AssertionError("assert 1 == 2")+    except AssertionError as e:+        exc = e+    tb = exc.__traceback__+    assert tb is not None+    exc_info = (AssertionError, exc, tb)+    excinfo = ExceptionInfo[AssertionError].for_later()+    excinfo.fill_unfilled(exc_info)+    assert excinfo.exconly(tryshort=True) == "assert 1 == 2"+    expected = ExceptionInfo.from_exc_info(exc_info)+    assert excinfo.exconly(tryshort=True) == expected.exconly(tryshort=True)++ def test_excinfo_repr_str() -> None:     with pytest.raises(ValueError) as excinfo1:         h()testing/test_subtests.pyindex 877e32b52..6593612f7 100644@@ -464,6 +464,31 @@ class TestUnittestSubTest:             ]         ) +    def test_failed_assert_strips_assertion_error(+        self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch+    ) -> None:+        """Failed assert statements inside TestCase.subTest are reported without+        the 'AssertionError: ' prefix, like regular test failures."""+        monkeypatch.setenv("COLUMNS", "120")+        pytester.makepyfile(+            """+            from unittest import TestCase++            class T(TestCase):+                def test_foo(self):+                    x = 1+                    with self.subTest("foo subtest"):+                        assert x == 2+            """+        )+        result = pytester.runpytest("-rf")+        result.stdout.fnmatch_lines(+            [+                "SUBFAILED*test_foo - assert 1 == 2",+            ]+        )+        result.stdout.no_fnmatch_line("*AssertionError: assert 1 == 2*")+     def test_passes(         self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch     ) -> None: 
Merged fix · PR #+64 −14
changelog/12175.bugfix.rstnew file mode 100644index 000000000..51b431df1@@ -0,0 +1 @@+:meth:`ExceptionInfo.exconly(tryshort=True) <pytest.ExceptionInfo.exconly>` now strips the ``AssertionError:`` prefix of failed ``assert`` statements for :class:`~pytest.ExceptionInfo` objects created by :func:`pytest.raises`, and failures inside :meth:`unittest.TestCase.subTest` report the same short crash line as regular tests.src/_pytest/_code/code.pyindex e7712c48b..f6d2f2767 100644@@ -504,13 +504,14 @@ class ExceptionInfo(Generic[E]):     _assert_start_repr: ClassVar = "AssertionError('assert "      _excinfo: tuple[type[E], E, TracebackType] | None-    _striptext: str+    # None means "derive from the exception when needed", see _get_striptext().+    _striptext: str | None     _traceback: Traceback | None      def __init__(         self,         excinfo: tuple[type[E], E, TracebackType] | None,-        striptext: str = "",+        striptext: str | None = None,         traceback: Traceback | None = None,         *,         _ispytest: bool = False,@@ -557,15 +558,9 @@ class ExceptionInfo(Generic[E]):         exprinfo: str | None = None,     ) -> ExceptionInfo[E]:         """Like :func:`from_exception`, but using old-style exc_info tuple."""-        _striptext = ""-        if exprinfo is None and isinstance(exc_info[1], AssertionError):-            exprinfo = getattr(exc_info[1], "msg", None)-            if exprinfo is None:-                exprinfo = saferepr(exc_info[1])-            if exprinfo and exprinfo.startswith(cls._assert_start_repr):-                _striptext = "AssertionError: "--        return cls(exc_info, _striptext, _ispytest=True)+        # An explicit exprinfo has always disabled stripping.+        striptext = None if exprinfo is None else ""+        return cls(exc_info, striptext, _ispytest=True)      @classmethod     def from_current(cls, exprinfo: str | None = None) -> ExceptionInfo[BaseException]:@@ -621,6 +616,18 @@ class ExceptionInfo(Generic[E]):         )         return self._excinfo[2] +    def _get_striptext(self) -> str:+        if self._striptext is None:+            self._striptext = ""+            exc = self.value+            if isinstance(exc, AssertionError):+                exprinfo = getattr(exc, "msg", None)+                if exprinfo is None:+                    exprinfo = saferepr(exc)+                if exprinfo and exprinfo.startswith(self._assert_start_repr):+                    self._striptext = "AssertionError: "+        return self._striptext+     @property     def typename(self) -> str:         """The type name of the exception."""@@ -653,8 +660,10 @@ class ExceptionInfo(Generic[E]):         information for SyntaxError's.          :param tryshort:-            If true, and the exception is an AssertionError, strip-            'AssertionError: ' from the beginning.+            If true, and the exception is an AssertionError raised by a+            failing ``assert`` statement (with assertion rewriting active),+            strip 'AssertionError: ' from the beginning, leaving just the+            ``assert ...`` explanation.         """          def _get_single_subexc(@@ -678,7 +687,7 @@ class ExceptionInfo(Generic[E]):         text = "".join(lines)         text = text.rstrip()         if tryshort:-            text = text.removeprefix(self._striptext)+            text = text.removeprefix(self._get_striptext())         return text      def errisinstance(self, exc: EXCEPTION_OR_MORE) -> bool:testing/code/test_excinfo.pyindex ec9f584df..5a8bc5947 100644@@ -340,6 +340,23 @@ class TestTraceback_f_g_h:         assert excinfo._getreprcrash() is None  +def test_excinfo_exconly_tryshort_assert() -> None:+    """Rewritten asserts lose their 'AssertionError: ' prefix, also when the+    ExceptionInfo comes from pytest.raises (#12175)."""+    x = 1+    with pytest.raises(AssertionError) as excinfo:+        assert x == 2+    assert excinfo.exconly(tryshort=True) == "assert 1 == 2"+    assert ExceptionInfo.from_exception(excinfo.value).exconly(tryshort=True) == (+        "assert 1 == 2"+    )++    # Explicit AssertionErrors keep their type name.+    with pytest.raises(AssertionError) as excinfo:+        raise AssertionError("message")+    assert excinfo.exconly(tryshort=True) == "AssertionError: message"++ def test_excinfo_exconly():     with pytest.raises(ValueError) as excinfo:         h()testing/test_subtests.pyindex 877e32b52..a64897c6f 100644@@ -464,6 +464,29 @@ class TestUnittestSubTest:             ]         ) +    def test_assert_crash_line(self, pytester: pytest.Pytester) -> None:+        """Rewritten asserts in subtests report without 'AssertionError: ',+        like regular tests do (#12175)."""+        pytester.makepyfile(+            """+            from unittest import TestCase++            class T(TestCase):+                def test_foo(self):+                    x = 1+                    with self.subTest("foo subtest"):+                        assert x == 2+            """+        )+        result = pytester.runpytest("-rf")+        result.stdout.fnmatch_lines(+            [+                "E * assert 1 == 2",+                "SUBFAILED*test_foo - assert 1 == 2",+            ]+        )+        result.stdout.no_fnmatch_line("*AssertionError: assert*")+     def test_passes(         self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch     ) -> None: