From 485e16aec3be4df15a064bcfede3e88d3257d527 Mon Sep 17 00:00:00 2001 From: Gabriele Figueiredo Date: Wed, 28 Oct 2026 11:04:15 -0300 Subject: [PATCH 1/4] fix: show whitespace escape codes in string diffs (#10704) --- changelog/10704.bugfix.rst | 1 + src/_pytest/assertion/compare_text.py | 24 ++++++++++- testing/test_assertion.py | 58 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 changelog/10704.bugfix.rst diff --git a/changelog/10704.bugfix.rst b/changelog/10704.bugfix.rst new file mode 100644 index 00000000000..abe9b77f81e --- /dev/null +++ b/changelog/10704.bugfix.rst @@ -0,0 +1 @@ +Show whitespace escape codes (such as ``\t``, ``\r``, ``\v``, ``\f``, ``\u00a0``) in string comparison diffs when non-printable whitespace characters differ. diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index 9bee86a0b93..a4405bedaf3 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -11,6 +11,20 @@ from _pytest.compat import assert_never +_WHITESPACE_MAP = { + "\t": r"\t", + "\r": r"\r", + "\v": r"\v", + "\f": r"\f", + "\u00a0": r"\u00a0", +} + + +def _escape_whitespace(text: str) -> str: + """Substitui caracteres de espaço ocultos por suas representações literais.""" + return "".join(_WHITESPACE_MAP.get(ch, ch) for ch in text) + + def _compare_eq_text( left: str, right: str, @@ -81,13 +95,21 @@ def _diff_text( ) left = left[:-i] right = right[:-i] + keepends = True if left.isspace() or right.isspace(): left = repr(str(left)) right = repr(str(right)) yield "Strings contain only whitespace, escaping them using repr()" + left_lines = _cap_ndiff_input(left, keepends, truncation_budget) right_lines = _cap_ndiff_input(right, keepends, truncation_budget) + + # Escapa caracteres de espaço invisíveis (\t, \r, \v, \f, \u00a0) mantendo + # a estrutura original de quebras de linha para a exibição do diff. + left_lines = [_escape_whitespace(line) for line in left_lines] + right_lines = [_escape_whitespace(line) for line in right_lines] + # "right" is the expected base against which we compare "left", # see https://github.com/pytest-dev/pytest/issues/3333 yield from highlighter( @@ -131,4 +153,4 @@ def _notin_text( if line.startswith("+ "): yield " " + line[2:] else: - yield line + yield line \ No newline at end of file diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 035aad31c54..5d83142f16c 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -3155,3 +3155,61 @@ def test_order(): "test_order.py:*: AssertionError", ] ) + + +@pytest.mark.parametrize( + ("actual", "expected", "escape"), + [ + pytest.param("hello\tworld", "hello world", r"\t", id="tab"), + pytest.param("hello\tworld", "hello earth", r"\t", id="whitespace-and-text"), + pytest.param("hello\vworld", "hello world", r"\v", id="vertical-tab"), + pytest.param("hello\fworld", "hello world", r"\f", id="form-feed"), + pytest.param("hello\r\nworld", "hello\nworld", r"\r", id="carriage-return"), + pytest.param("hello\t", "hello ", r"\t", id="trailing-tab-vs-space"), + pytest.param("hello world", "hello\u00a0world", r"\u00a0", id="nbsp"), + ], +) +def test_reprcompare_whitespace_inside_text( + actual: str, expected: str, escape: str +) -> None: + lines = callequal(actual, expected) + assert lines is not None + + diff_lines = [ + line for line in lines + if line.startswith(("+", "-")) + ] + assert diff_lines + assert any(escape in line for line in diff_lines) + assert not any( + char in line + for line in diff_lines + for char in "\t\r\v\f" + ) + + +def test_reprcompare_whitespace_inside_text_identical() -> None: + lines = callequal("hello\tworld", "hello\tworld") + assert lines is not None + assert not any(line.startswith(("+", "-")) for line in lines) + + +def test_whitespace_diff_pytester(pytester: Pytester) -> None: + """Garante que códigos de escape para whitespace invisível sejam visíveis na saída do terminal (#10704).""" + p = pytester.makepyfile( + """ + def test_whitespace(): + expected = "hello\tworld" + actual = "hello world" + assert actual == expected + """ + ) + result = pytester.runpytest(str(p)) + result.stdout.fnmatch_lines( + [ + "*AssertionError: assert*", + "*- hello\\tworld*", + "*+ hello world*", + "*1 failed in*", + ] + ) \ No newline at end of file From 66fa6c93ef3e08e5ab166ef52d6ddedf9b79c53e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:19:28 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/compare_text.py | 6 +++--- testing/test_assertion.py | 13 +++---------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index a4405bedaf3..15deb11c8ff 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -21,7 +21,7 @@ def _escape_whitespace(text: str) -> str: - """Substitui caracteres de espaço ocultos por suas representações literais.""" + """Substitui characters de espaço ocultos por suas representações literais.""" return "".join(_WHITESPACE_MAP.get(ch, ch) for ch in text) @@ -105,7 +105,7 @@ def _diff_text( left_lines = _cap_ndiff_input(left, keepends, truncation_budget) right_lines = _cap_ndiff_input(right, keepends, truncation_budget) - # Escapa caracteres de espaço invisíveis (\t, \r, \v, \f, \u00a0) mantendo + # Escapa characters de espaço invisíveis (\t, \r, \v, \f, \u00a0) mantendo # a estrutura original de quebras de linha para a exibição do diff. left_lines = [_escape_whitespace(line) for line in left_lines] right_lines = [_escape_whitespace(line) for line in right_lines] @@ -153,4 +153,4 @@ def _notin_text( if line.startswith("+ "): yield " " + line[2:] else: - yield line \ No newline at end of file + yield line diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 5d83142f16c..71be8f2d5ec 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -3175,17 +3175,10 @@ def test_reprcompare_whitespace_inside_text( lines = callequal(actual, expected) assert lines is not None - diff_lines = [ - line for line in lines - if line.startswith(("+", "-")) - ] + diff_lines = [line for line in lines if line.startswith(("+", "-"))] assert diff_lines assert any(escape in line for line in diff_lines) - assert not any( - char in line - for line in diff_lines - for char in "\t\r\v\f" - ) + assert not any(char in line for line in diff_lines for char in "\t\r\v\f") def test_reprcompare_whitespace_inside_text_identical() -> None: @@ -3212,4 +3205,4 @@ def test_whitespace(): "*+ hello world*", "*1 failed in*", ] - ) \ No newline at end of file + ) From ef45081fe280408dce3834e7761ac6bcfd93b4bb Mon Sep 17 00:00:00 2001 From: Gabriele Figueiredo Date: Fri, 4 Dec 2026 18:41:08 -0300 Subject: [PATCH 3/4] updates made following feedback git add src/_pytest/assertion/compare_text.py git rebase --continuegit add src/_pytest/assertion/compare_text.py git rebase --contin --- src/_pytest/assertion/compare_text.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index 15deb11c8ff..8d7536df368 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -21,7 +21,7 @@ def _escape_whitespace(text: str) -> str: - """Substitui characters de espaço ocultos por suas representações literais.""" + """Replace hidden whitespace characters with their literal escape representations.""" return "".join(_WHITESPACE_MAP.get(ch, ch) for ch in text) @@ -95,7 +95,6 @@ def _diff_text( ) left = left[:-i] right = right[:-i] - keepends = True if left.isspace() or right.isspace(): left = repr(str(left)) @@ -105,15 +104,15 @@ def _diff_text( left_lines = _cap_ndiff_input(left, keepends, truncation_budget) right_lines = _cap_ndiff_input(right, keepends, truncation_budget) - # Escapa characters de espaço invisíveis (\t, \r, \v, \f, \u00a0) mantendo - # a estrutura original de quebras de linha para a exibição do diff. - left_lines = [_escape_whitespace(line) for line in left_lines] - right_lines = [_escape_whitespace(line) for line in right_lines] - + # Escape invisible whitespace characters (\t, \r, \v, \f, \u00a0) while + # preserving the original line-break structure for diff display. # "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.strip("\n") for line in ndiff(right_lines, left_lines)), + "\n".join( + _escape_whitespace(line.strip("\n")) + for line in ndiff(right_lines, left_lines) + ), lexer="diff", ).splitlines() @@ -153,4 +152,4 @@ def _notin_text( if line.startswith("+ "): yield " " + line[2:] else: - yield line + yield line \ No newline at end of file From 55f291d79aa9c299ac0f02a6dfab447fc52d143b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 21:59:25 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/compare_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/assertion/compare_text.py b/src/_pytest/assertion/compare_text.py index 8d7536df368..62310473e2e 100644 --- a/src/_pytest/assertion/compare_text.py +++ b/src/_pytest/assertion/compare_text.py @@ -152,4 +152,4 @@ def _notin_text( if line.startswith("+ "): yield " " + line[2:] else: - yield line \ No newline at end of file + yield line