Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
picnixz opened this issue Apr 1, 2024 · 3 comments
Open

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

picnixz opened this issue Apr 1, 2024 · 3 comments
Labels
type: bug problem that needs to be addressed

Comments

@picnixz
Copy link

picnixz commented Apr 1, 2024

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
@RonnyPfannschmidt
Copy link
Member

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

@RonnyPfannschmidt RonnyPfannschmidt changed the title Incorrect behaviour of ExceptionInfo.exconly() ExceptionInfo.for_later() wont add assertion strip text Apr 2, 2024
@RonnyPfannschmidt RonnyPfannschmidt added the type: bug problem that needs to be addressed label Apr 2, 2024
@RonnyPfannschmidt
Copy link
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

@picnixz
Copy link
Author

picnixz commented Apr 4, 2024

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: ')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

2 participants