MCPcopy
hub / github.com/pytest-dev/pytest / exconly

Method exconly

src/_pytest/_code/code.py:646–681  ·  view source on GitHub ↗

Return the exception as a string. This is usually a single line "<exception type>: <exception str>", but may also include additional lines for the exception notes, and detailed information for SyntaxError's. :param tryshort: If true, and the exception is

(self, tryshort: bool = False)

Source from the content-addressed store, hash-verified

644 return f"<{self.__class__.__name__} {saferepr(self._excinfo[1])} tblen={len(self.traceback)}>"
645
646 def exconly(self, tryshort: bool = False) -> str:
647 """Return the exception as a string.
648
649 This is usually a single line "<exception type>: <exception str>", but
650 may also include additional lines for the exception notes, and detailed
651 information for SyntaxError&#x27;s.
652
653 :param tryshort:
654 If true, and the exception is an AssertionError, strip
655 'AssertionError: ' from the beginning.
656 """
657
658 def _get_single_subexc(
659 eg: BaseExceptionGroup[BaseException],
660 ) -> BaseException | None:
661 if len(eg.exceptions) != 1:
662 return None
663 if isinstance(e := eg.exceptions[0], BaseExceptionGroup):
664 return _get_single_subexc(e)
665 return e
666
667 if (
668 tryshort
669 and isinstance(self.value, BaseExceptionGroup)
670 and (subexc := _get_single_subexc(self.value)) is not None
671 ):
672 return f"{subexc!r} [single exception in {type(self.value).__name__}]"
673
674 lines = format_exception_only(self.value)
675 # The lines already include line separators.
676 text = "".join(lines)
677 text = text.rstrip()
678 if tryshort:
679 if text.startswith(self._striptext):
680 text = text[len(self._striptext) :]
681 return text
682
683 def errisinstance(self, exc: EXCEPTION_OR_MORE) -> bool:
684 """Return True if the exception is an instance of exc.

Callers 7

_getreprcrashMethod · 0.95
importtestmoduleFunction · 0.80
get_exconlyMethod · 0.80
test_pytest_failFunction · 0.80
test_excinfo_exconlyFunction · 0.80

Calls 1

joinMethod · 0.80

Tested by 3

test_pytest_failFunction · 0.64
test_excinfo_exconlyFunction · 0.64