Repository: pytest-dev/pytest
File: src/_pytest/_code/code.py (Python)

Code before:
        """The traceback."""
        if self._traceback is None:
            self._traceback = Traceback(self.tb)
        return self._traceback

    @traceback.setter
    def traceback(self, value: Traceback) -> None:
        self._traceback = value

    def __repr__(self) -> str:
        if self._excinfo is None:
            return "<ExceptionInfo for raises contextmanager>"
        return f"<{self.__class__.__name__} {saferepr(self._excinfo[1])} tblen={len(self.traceback)}>"

    def exconly(self, tryshort: bool = False) -> str:
        """Return the exception as a string.

        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).
        """

        def _get_single_subexc(
            eg: BaseExceptionGroup[BaseException],
        ) -> BaseException | None:
            res: BaseException | None = None
            for subexc in eg.exceptions:
                if res is not None:
                    return None

                if isinstance(subexc, BaseExceptionGroup):
                    res = _get_single_subexc(subexc)
                    if res is None:
                        # there were multiple exceptions in the subgroup
                        return None
                else:
                    res = subexc
            return res

        if (
            tryshort
            and isinstance(self.value, BaseExceptionGroup)
            and (subexc := _get_single_subexc(self.value)) is not None
        ):
            return f"[in {type(self.value).__name__}] {subexc!r}"

        lines = format_exception_only(self.type, self.value)
        text = "".join(lines)
        text = text.rstrip()
        if tryshort:

Review comment (suggestion):
Simpler implementation:
```python
def _get_single_subexc(
    eg: BaseExceptionGroup[BaseException],
) -> BaseException | None:
    if len(eg.exceptions) != 1:
        return None
    if isinstance(e := eg.exceptions[0], BaseExceptionGroup):
        return _get_single_subexc(e)
    return e
```

Code after:
        """The traceback."""
        if self._traceback is None:
            self._traceback = Traceback(self.tb)
        return self._traceback

    @traceback.setter
    def traceback(self, value: Traceback) -> None:
        self._traceback = value

    def __repr__(self) -> str:
        if self._excinfo is None:
            return "<ExceptionInfo for raises contextmanager>"
        return f"<{self.__class__.__name__} {saferepr(self._excinfo[1])} tblen={len(self.traceback)}>"

    def exconly(self, tryshort: bool = False) -> str:
        """Return the exception as a string.

        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).
        """

        def _get_single_subexc(
            eg: BaseExceptionGroup[BaseException],
        ) -> BaseException | None:
            if len(eg.exceptions) != 1:
                return None
            if isinstance(e := eg.exceptions[0], BaseExceptionGroup):
                return _get_single_subexc(e)
            return e

        if (
            tryshort
            and isinstance(self.value, BaseExceptionGroup)
            and (subexc := _get_single_subexc(self.value)) is not None
        ):
            return f"{subexc!r} [single exception in {type(self.value).__name__}]"

        lines = format_exception_only(self.type, self.value)
        text = "".join(lines)
        text = text.rstrip()
        if tryshort:
            if text.startswith(self._striptext):
                text = text[len(self._striptext) :]
        return text

    def errisinstance(self, exc: EXCEPTION_OR_MORE) -> bool:
        """Return True if the exception is an instance of exc.

        Consider using ``isinstance(excinfo.value, exc)`` instead.