Skip to content

Fix ExceptionInfo.for_later() not populating _striptext#14206

Open
Fridayai700 wants to merge 3 commits intopytest-dev:mainfrom
Fridayai700:fix-for-later-striptext
Open

Fix ExceptionInfo.for_later() not populating _striptext#14206
Fridayai700 wants to merge 3 commits intopytest-dev:mainfrom
Fridayai700:fix-for-later-striptext

Conversation

@Fridayai700
Copy link

Summary

Fixes #12175.

When ExceptionInfo is created via for_later() (as used by pytest.raises), the _striptext attribute is never populated. This causes 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 with those created via from_exc_info().

Changes

  • fill_unfilled() now populates _striptext when the exception is an AssertionError whose saferepr starts with _assert_start_repr, matching the logic in from_exc_info()
  • Added test test_excinfo_for_later_strips_assertion verifying the fix
  • Added changelog entry

Test plan

  • New test verifies exconly(tryshort=True) strips AssertionError: prefix for rewritten assertions caught via pytest.raises
  • Existing test_excinfo_for_later and test_excinfo_exconly still pass

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>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Feb 18, 2026
"""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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@Fridayai700
Copy link
Author

Good point — extracted the logic into a _compute_striptext() classmethod that both from_exc_info() and fill_unfilled() now call. Pushed the update.

@Fridayai700
Copy link
Author

Note: the second commit (0ce07e6) already refactors this — it extracts the logic into a _compute_striptext() classmethod, so both from_exc_info() and fill_unfilled() call the same shared implementation. The diff you are commenting on is the first commit; the dedup is in the follow-up.

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>
@Fridayai700
Copy link
Author

Added two more test cases to cover the _compute_striptext edge cases (non-AssertionError path and AssertionError with explicit .msg attribute). This should fix the codecov/patch coverage gap.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExceptionInfo.for_later() wont add assertion strip text

2 participants

Comments