Skip to content

Cap text diff computation for huge strings in non-verbose mode#14749

Closed
Zac-HD wants to merge 1 commit into
pytest-dev:mainfrom
Zac-HD:claude/pytest-issue-12406-l9uasm
Closed

Cap text diff computation for huge strings in non-verbose mode#14749
Zac-HD wants to merge 1 commit into
pytest-dev:mainfrom
Zac-HD:claude/pytest-issue-12406-l9uasm

Conversation

@Zac-HD

@Zac-HD Zac-HD commented Jul 21, 2026

Copy link
Copy Markdown
Member

At the default verbosity the displayed assertion explanation is truncated to a handful of lines, but _diff_text always ran difflib.ndiff over the full strings first. For multi-million-line strings this could take hours (difflib's runtime blows up on large inputs), only for almost all of the output to be discarded.

Now, when verbose < 1, the diff is computed over at most the first 100 lines of each side (after the existing identical leading/trailing character skipping), with a message pointing at -v to get the full diff. This mirrors the existing behaviour of skipping identical leading/trailing characters unless -v is given, and the precedent of _compare_eq_iterable not computing a full diff at low verbosity.

The message starts with "Skipping" so that _notin_text's existing filtering drops it there, like the other skip hints.

Fixes #12406

At the default verbosity the displayed assertion explanation is
truncated to a handful of lines, but ``_diff_text`` always ran
``difflib.ndiff`` over the full strings first. For multi-million-line
strings this could take hours (difflib's runtime blows up on large
inputs), only for almost all of the output to be discarded.

Now, when ``verbose < 1``, the diff is computed over at most the first
100 lines of each side (after the existing identical leading/trailing
character skipping), with a message pointing at ``-v`` to get the full
diff. This mirrors the existing behaviour of skipping identical
leading/trailing characters unless ``-v`` is given, and the precedent
of ``_compare_eq_iterable`` not computing a full diff at low verbosity.

The message starts with "Skipping" so that ``_notin_text``'s existing
filtering drops it there, like the other skip hints.

Fixes pytest-dev#12406

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVRWXxfTLrU4B95jeoeuHN
@Zac-HD
Zac-HD requested a review from RonnyPfannschmidt July 21, 2026 17:27
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 21, 2026
@Pierre-Sassoulas

Pierre-Sassoulas commented Jul 21, 2026

Copy link
Copy Markdown
Member

More conservative approach than #14543 that keep the exact output even when it would take a lot of time to generate ? (Might not fix #8998)

@Pierre-Sassoulas

Copy link
Copy Markdown
Member

Also, if you're interested in making assertion message faster there's also #14523 that is very impactful on everything but the ndiff part when the verbosity is low and the assert message will be truncated :)

@Zac-HD

Zac-HD commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Closing this in favor of #14543; my patch would not handle the case of very long individual lines, and I'm happy to defer to work already in flight.

@Zac-HD Zac-HD closed this Jul 21, 2026
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.

text diffs on huge files are slow

3 participants