-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[refactor] Make base assertion comparisons return an iterator instead of a list of string #14546
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from collections.abc import Iterator | ||
| from collections.abc import Sequence | ||
| from typing import Literal | ||
| from unicodedata import normalize | ||
|
|
@@ -139,8 +140,19 @@ def assertrepr_compare( | |
| verbose: int, | ||
| highlighter: _HighlightFunc, | ||
| assertion_text_diff_style: _AssertionTextDiffStyle, | ||
| ) -> list[str] | None: | ||
| """Return specialised explanations for some operators/operands.""" | ||
| ) -> Iterator[str]: | ||
| """Yield specialised explanations for some operators/operands. | ||
|
|
||
| The first line yielded is always the summary (``left op right``); | ||
| subsequent lines are the per-line explanation. Yields nothing when no | ||
| specialised explanation applies, which lets consumers map an empty | ||
| iterator to "no explanation" without materialising anything. | ||
|
|
||
| The iterator is lazy on purpose: a streaming consumer (e.g. the | ||
|
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. We should drop the "e.g." part since it doesn't hold as of this commit. |
||
| truncator in ``pytest_assertrepr_compare``) can stop pulling lines as | ||
| soon as it has enough to show, so an enormous diff doesn't have to be | ||
| built in full just to be thrown away. | ||
| """ | ||
| # Strings which normalize equal are often hard to distinguish when printed; use ascii() to make this easier. | ||
| # See issue #3246. | ||
| use_ascii = ( | ||
|
|
@@ -164,37 +176,41 @@ def assertrepr_compare( | |
|
|
||
| summary = f"{left_repr} {op} {right_repr}" | ||
|
|
||
| explanation = None | ||
| summary_yielded = False | ||
|
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. Would move this above the |
||
| try: | ||
| if op == "==": | ||
| explanation = _compare_eq_any( | ||
| source: Iterator[str] = _compare_eq_any( | ||
| left, | ||
| right, | ||
| highlighter, | ||
| verbose, | ||
| assertion_text_diff_style, | ||
| ) | ||
| elif op == "not in": | ||
| if istext(left) and istext(right): | ||
| explanation = list(_notin_text(left, right, verbose)) | ||
| elif op in {"!=", ">=", "<=", ">", "<"}: | ||
| if isset(left) and isset(right): | ||
| explanation = SET_COMPARISON_FUNCTIONS[op]( | ||
| left, right, highlighter, verbose | ||
| ) | ||
|
|
||
| elif op == "not in" and istext(left) and istext(right): | ||
| source = _notin_text(left, right, verbose) | ||
| elif op in {"!=", ">=", "<=", ">", "<"} and isset(left) and isset(right): | ||
| source = iter( | ||
| SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose) | ||
| ) | ||
| else: | ||
| source = iter(()) | ||
|
|
||
| for line in source: | ||
|
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. I suggest a comment like this: Only yield summary if there is a detailed explanation. Make sure there's a separating empty line after the summary. Usually I avoid comments which just describe what the code does, but in this case it took me a bit of effort to understand, so I think a comment would be helpful. |
||
| if not summary_yielded: | ||
| yield summary | ||
| if line != "": | ||
| yield "" | ||
| summary_yielded = True | ||
| yield line | ||
| except outcomes.Exit: | ||
| raise | ||
| except Exception: | ||
| repr_crash = _pytest._code.ExceptionInfo.from_current()._getreprcrash() | ||
| explanation = [ | ||
| f"(pytest_assertion plugin: representation of details failed: {repr_crash}.", | ||
| " Probably an object has a faulty __repr__.)", | ||
| ] | ||
|
|
||
| if not explanation: | ||
| return None | ||
|
|
||
| if explanation[0] != "": | ||
| explanation = ["", *explanation] | ||
| return [summary, *explanation] | ||
| if not summary_yielded: | ||
| yield summary | ||
| yield "" | ||
| summary_yielded = True | ||
| yield ( | ||
| f"(pytest_assertion plugin: representation of details failed: {repr_crash}." | ||
| ) | ||
| yield " Probably an object has a faulty __repr__.)" | ||
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.
"are the per-line explanation" -> "is the detailed explanation".
I don't think "per-line" is entirely accurate.