Skip to content

ExceptionInfo.for_later() wont add assertion strip text #12175

Closed

Description

@picnixz

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:

def test_excinfo_exconly():
excinfo = pytest.raises(ValueError, h)
assert excinfo.exconly().startswith("ValueError")
with pytest.raises(ValueError) as excinfo:
raise ValueError("hello\nworld")
msg = excinfo.exconly(tryshort=True)
assert msg.startswith("ValueError")
assert msg.endswith("world")

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

Activity

  1. RonnyPfannschmidt commented on Apr 2, 2024

    @RonnyPfannschmidt
    Member

    _striptext = ""
    seems to fill the strip text incorrectly in this case

  2. changed the title [-]Incorrect behaviour of ``ExceptionInfo.exconly()``[/-] [+]``ExceptionInfo.for_later()`` wont add assertion strip text[/+] on Apr 2, 2024
  3. RonnyPfannschmidt commented on Apr 2, 2024

    @RonnyPfannschmidt
    Member

    the root cause is the for_later helper for making a instance before the exception

    we never fill in the strip text for that case

    if you create a new exception info object from the value of the one captured the new instance would behave correct

  4. picnixz commented on Apr 4, 2024

    @picnixz
    Author

    By the way, here is the current workaround I am using in Sphinx (probably imperfect but suitable for my needs):

    def parse_excinfo(excinfo: ExceptionInfo[AssertionError]) -> str:
        assert excinfo.type is AssertionError
        assert excinfo.value is not None
        return str(excinfo.value).removeprefix('AssertionError: ')
  5. added a commit that references this issue on Feb 18, 2026
    055b8fd
  6. added a commit that references this issue on Oct 8, 2026
    9260a4b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions