Repository navigation
Derive ExceptionInfo strip text lazily from the exception - #15149
RonnyPfannschmidt wants to merge 3 commits into
Conversation
ExceptionInfo only computed its "AssertionError: " strip text in
from_exc_info(). Instances created via for_later()/fill_unfilled()
(pytest.raises, RaisesGroup) or constructed directly (unittest
addSubTest) never got it, so exconly(tryshort=True) kept the prefix
for rewritten asserts.
The strip text is a pure function of the exception, so derive it on
first use instead of at construction. An explicit exprinfo keeps its
historical meaning of disabling stripping.
Also correct the exconly() docstring: only failing assert statements
are shortened, an explicit AssertionError("msg") keeps its type name.
Fixes #12175
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RPVUqQy85BXtPFPctRc71n
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RPVUqQy85BXtPFPctRc71n
|
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Why I don't think this PR causes it: the diff only changes how What I can't explain yet is why it fails twice here while other PRs pass this job today. Possible fix, outside this PR's scope: assert |
Requested by Ronny · project thread
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Fixes #12175.
Before:
exconly(tryshort=True)kept theAssertionError:prefix for a failingassertwhen theExceptionInfocame frompytest.raises/RaisesGroup(for_later()+fill_unfilled()), or from unittest'saddSubTest(direct construction). A failingassertinsideTestCase.subTesttherefore reportedE AssertionError: assert 1 == 2andSUBFAILED ... - AssertionError: assert 1 == 2, unlike a regular test.After: every
ExceptionInfostrips the prefix for rewritten asserts, sopytest.raisesand unittest subtests match regular test output.How
The strip text depends only on the exception value, so it is now derived on first use in
exconly()(_get_striptext()) instead of being computed infrom_exc_info()._striptext=Nonemeans "derive"; an explicitexprinfokeeps its historical effect of disabling stripping. Theexconly()docstring now says what the code actually does: only failingassertstatements are shortened, an explicitAssertionError("message")keeps its type name.Why this differs from #14206, #15089 and #15148
Those three PRs (all closed) extracted a
_compute_striptext()helper and called it again fromfill_unfilled(). That keeps the eager design, where every constructor has to remember to set the strip text:ExceptionInfo(exc_info, _ispytest=True)construction inunittest.addSubTest, so the subtest crash line stays wrong. This PR covers that path and tests it intesting/test_subtests.py.raise AssertionError("message"). That case keeps its prefix on every path, includingfrom_exception(), by design: pytest's failure output relies on only rewritten asserts being shortened. The real defect there is the docstring, which promised stripping for anyAssertionError. This PR fixes the docstring rather than changing reporting behavior.Checklist
main).Fixes #12175in the description and commit.Co-authored-bycommit trailers.changelog/12175.bugfix.rst.AUTHORS: already listed.Full suite passed locally on Python 3.13 (4637 passed), plus
pre-commiton the changed files, including mypy.🤖 Generated with Claude Code
https://claude.ai/code/session_01RPVUqQy85BXtPFPctRc71n
Generated by Claude Code