Skip to content

Commit 0ce07e6

Browse files
worksbyfridayclaude
andcommitted
Deduplicate _striptext logic into _compute_striptext classmethod
Address review feedback: extract the AssertionError prefix detection into a shared _compute_striptext() classmethod used by both from_exc_info() and fill_unfilled(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 055b8fd commit 0ce07e6

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/_pytest/_code/code.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,20 @@ def from_exception(
545545
exc_info = (type(exception), exception, exception.__traceback__)
546546
return cls.from_exc_info(exc_info, exprinfo)
547547

548+
@classmethod
549+
def _compute_striptext(
550+
cls,
551+
exc_info: tuple[type[E], E, TracebackType],
552+
) -> str:
553+
"""Determine if AssertionError prefix should be stripped from output."""
554+
if isinstance(exc_info[1], AssertionError):
555+
exprinfo = getattr(exc_info[1], "msg", None)
556+
if exprinfo is None:
557+
exprinfo = saferepr(exc_info[1])
558+
if exprinfo and exprinfo.startswith(cls._assert_start_repr):
559+
return "AssertionError: "
560+
return ""
561+
548562
@classmethod
549563
def from_exc_info(
550564
cls,
@@ -553,12 +567,8 @@ def from_exc_info(
553567
) -> ExceptionInfo[E]:
554568
"""Like :func:`from_exception`, but using old-style exc_info tuple."""
555569
_striptext = ""
556-
if exprinfo is None and isinstance(exc_info[1], AssertionError):
557-
exprinfo = getattr(exc_info[1], "msg", None)
558-
if exprinfo is None:
559-
exprinfo = saferepr(exc_info[1])
560-
if exprinfo and exprinfo.startswith(cls._assert_start_repr):
561-
_striptext = "AssertionError: "
570+
if exprinfo is None:
571+
_striptext = cls._compute_striptext(exc_info)
562572

563573
return cls(exc_info, _striptext, _ispytest=True)
564574

@@ -591,12 +601,7 @@ def fill_unfilled(self, exc_info: tuple[type[E], E, TracebackType]) -> None:
591601
"""Fill an unfilled ExceptionInfo created with ``for_later()``."""
592602
assert self._excinfo is None, "ExceptionInfo was already filled"
593603
self._excinfo = exc_info
594-
if isinstance(exc_info[1], AssertionError):
595-
exprinfo = getattr(exc_info[1], "msg", None)
596-
if exprinfo is None:
597-
exprinfo = saferepr(exc_info[1])
598-
if exprinfo and exprinfo.startswith(self._assert_start_repr):
599-
self._striptext = "AssertionError: "
604+
self._striptext = self._compute_striptext(exc_info)
600605

601606
@property
602607
def type(self) -> type[E]:

0 commit comments

Comments
 (0)