-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Use streaming in all assertion comparisons consumers #14523
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
Merged
Pierre-Sassoulas
merged 6 commits into
pytest-dev:main
from
Pierre-Sassoulas:stream-comparisons
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
630a96e
[refactor] Take a Config instead of an Item in _get_truncation_parame…
Pierre-Sassoulas e375413
[perf] Cap the dict extra-items diff by the truncation budget
Pierre-Sassoulas 9719431
[refactor] Stream assertion explanations through truncation
Pierre-Sassoulas fbdae4e
[perf] Cap all comparison formatting by the truncation budget
Pierre-Sassoulas 0e16f22
[test] Cover streaming truncation and guard against O(N) work
Pierre-Sassoulas 8fe130e
[test] Cover the dispatcher budget wiring and plaintext fallback
Pierre-Sassoulas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Large assertion comparison diffs are now built lazily and capped to the | ||
| truncation budget, so a huge diff is no longer formatted in full just to | ||
| be truncated. As a result the truncation footer no longer | ||
| reports the exact number of hidden lines. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| from _pytest._io.pprint import PrettyPrinter | ||
| from _pytest._io.saferepr import saferepr | ||
| from _pytest.assertion._typing import _HighlightFunc | ||
| from _pytest.assertion._typing import NO_TRUNCATION_BUDGET | ||
| from _pytest.assertion._typing import TruncationBudget | ||
| from _pytest.compat import running_on_ci | ||
|
|
||
|
|
||
|
|
@@ -15,26 +17,30 @@ def _compare_eq_iterable( | |
| right: Iterable[object], | ||
| highlighter: _HighlightFunc, | ||
| verbose: int = 0, | ||
| truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET, | ||
| ) -> Iterator[str]: | ||
| if verbose <= 0 and not running_on_ci(): | ||
| yield "Use -v to get more diff" | ||
| return | ||
| # dynamic import to speedup pytest | ||
| import difflib | ||
|
|
||
| left_formatting = PrettyPrinter().pformat(left).splitlines() | ||
| right_formatting = PrettyPrinter().pformat(right).splitlines() | ||
| pp = PrettyPrinter() | ||
| # ``pformat_lines`` spells an unbounded dimension ``None``; the budget spells it ``0``. | ||
| max_lines = truncation_budget.max_lines or None | ||
| max_chars = truncation_budget.max_chars or None | ||
| left_formatting = pp.pformat_lines(left, max_lines=max_lines, max_chars=max_chars) | ||
| right_formatting = pp.pformat_lines(right, max_lines=max_lines, max_chars=max_chars) | ||
|
|
||
| yield "" | ||
| yield "Full diff: (-: missing in left side, +: extra in left side)" | ||
| # "right" is the expected base against which we compare "left", | ||
| # see https://github.com/pytest-dev/pytest/issues/3333 | ||
| yield from highlighter( | ||
| "\n".join( | ||
| line.rstrip() for line in difflib.ndiff(right_formatting, left_formatting) | ||
| ), | ||
| lexer="diff", | ||
| ).splitlines() | ||
| # Highlight each ndiff line individually so the streaming truncator can | ||
| # stop pulling from ``difflib.ndiff`` once its budget is full; the diff | ||
| # lexer is line-oriented, so per-line highlighting is equivalent. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If pygments/pygments#1043 is ever resolved, this won't be correct anymore. But I guess it's fine for now. |
||
| for line in difflib.ndiff(right_formatting, left_formatting): | ||
| yield highlighter(line.rstrip(), lexer="diff") | ||
|
|
||
|
|
||
| def _compare_eq_sequence( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unfortunate loss, though I understand why it's needed. I wonder whether there is a way to preserve it in some form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could keep it for the already materialized list that we get from plugins (I invested time in this algo, i'm also sad to see it go :D )