Fix ExceptionInfo.for_later() not populating _striptext#14206
Fix ExceptionInfo.for_later() not populating _striptext#14206Fridayai700 wants to merge 3 commits intopytest-dev:mainfrom
Conversation
When ExceptionInfo is created via for_later() and later filled with fill_unfilled(), the _striptext attribute was never populated. This caused exconly(tryshort=True) to not strip the "AssertionError: " prefix for rewritten assertions. The fix applies the same _striptext logic from from_exc_info() inside fill_unfilled(), so that ExceptionInfo objects created through the for_later() path behave consistently. Fixes pytest-dev#12175. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
src/_pytest/_code/code.py
Outdated
| """Fill an unfilled ExceptionInfo created with ``for_later()``.""" | ||
| assert self._excinfo is None, "ExceptionInfo was already filled" | ||
| self._excinfo = exc_info | ||
| if isinstance(exc_info[1], AssertionError): |
There was a problem hiding this comment.
i'm a bit torn, we don’t repeat this often enough for the rule of 3 to apply but i feels this logic should be there only once
There was a problem hiding this comment.
The logic is already centralized in _compute_striptext() (line 549) — both from_exc_info() and fill_unfilled() call that same classmethod. The two call sites are just one-liners invoking the shared implementation.
If you would prefer a different approach — like having fill_unfilled() delegate to from_exc_info() internally rather than having two separate call sites — I am happy to refactor.
Address review feedback: extract the AssertionError prefix detection into a shared _compute_striptext() classmethod used by both from_exc_info() and fill_unfilled(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Good point — extracted the logic into a |
|
Note: the second commit (0ce07e6) already refactors this — it extracts the logic into a |
Cover non-AssertionError path (returns "") and AssertionError with explicit .msg attribute that doesn't match _assert_start_repr. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Added two more test cases to cover the The readthedocs failure appears transient — other recent PRs (#14210, #14211) pass RTD fine, and the error is a generic 'Unknown problem' rather than a specific Sphinx issue. The changelog cross-references match the patterns used in the rest of the project. Hopefully it resolves on this push. |
Summary
Fixes #12175.
When
ExceptionInfois created viafor_later()(as used bypytest.raises), the_striptextattribute is never populated. This causesexconly(tryshort=True)to not strip theAssertionError:prefix for rewritten assertions.The fix applies the same
_striptextlogic fromfrom_exc_info()insidefill_unfilled(), so thatExceptionInfoobjects created through thefor_later()path behave consistently with those created viafrom_exc_info().Changes
fill_unfilled()now populates_striptextwhen the exception is anAssertionErrorwhosesafereprstarts with_assert_start_repr, matching the logic infrom_exc_info()test_excinfo_for_later_strips_assertionverifying the fixTest plan
exconly(tryshort=True)stripsAssertionError:prefix for rewritten assertions caught viapytest.raisestest_excinfo_for_laterandtest_excinfo_exconlystill pass