From e60e7ee59c71492d4a887fdc6775ae2fff687ec8 Mon Sep 17 00:00:00 2001 From: Reagan Date: Thu, 15 Aug 2024 22:29:12 -0700 Subject: [PATCH 1/7] changes --- src/_pytest/assertion/util.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 4dc1af4af03..21a7912e17a 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -501,10 +501,21 @@ def _compare_eq_dict( if diff: explanation += ["Differing items:"] for k in diff: + left_val = left[k] + right_val = right[k] + if issequence(left_val) and issequence(right_val): + # explanation.append(saferepr("BLAHBLAH: string")) + expl = _compare_eq_sequence( + left_val, right_val, lambda item: item, verbose + ) + expl = str("".join(expl)) + explanation += [highlighter(saferepr({k: expl}))] + continue + explanation += [ - highlighter(saferepr({k: left[k]})) + highlighter(saferepr({k: left_val})) + " != " - + highlighter(saferepr({k: right[k]})) + + highlighter(saferepr({k: right_val})) ] extra_left = set_left - set_right len_extra_left = len(extra_left) From 1d3d99e581f1e98bf0be221b6f9f9c5bac4322df Mon Sep 17 00:00:00 2001 From: Reagan Date: Thu, 15 Aug 2024 22:36:34 -0700 Subject: [PATCH 2/7] test --- testing/test_assertion.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 31192df0f6f..5a22bc0a007 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -741,6 +741,12 @@ def test_dict_different_items(self) -> None: " }", ] + def test_dict_sequence_items(self) -> None: + value1 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 1]} + value2 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 2]} + lines = callequal(value1, value2, verbose=0) + assert "{'asdf': 'At index 7 diff: 1 != 2'}" in "".join(lines) + def test_sequence_different_items(self) -> None: lines = callequal((1, 2), (3, 4, 5), verbose=2) assert lines == [ From 5210674742ec99b465f7acdadf6899e5ab11a471 Mon Sep 17 00:00:00 2001 From: Reagan Date: Thu, 15 Aug 2024 22:50:48 -0700 Subject: [PATCH 3/7] changelog --- changelog/11980.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/11980.improvement.rst diff --git a/changelog/11980.improvement.rst b/changelog/11980.improvement.rst new file mode 100644 index 00000000000..f8c2f42ceda --- /dev/null +++ b/changelog/11980.improvement.rst @@ -0,0 +1 @@ +Improves `AssertionError` error message for sequences inside dictionaries when not running verbose mode. \ No newline at end of file From 6c5a720eee20b09a1fb2132caffe26eb16a6803e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Aug 2024 05:51:52 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/11980.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/11980.improvement.rst b/changelog/11980.improvement.rst index f8c2f42ceda..ddeea26e823 100644 --- a/changelog/11980.improvement.rst +++ b/changelog/11980.improvement.rst @@ -1 +1 @@ -Improves `AssertionError` error message for sequences inside dictionaries when not running verbose mode. \ No newline at end of file +Improves `AssertionError` error message for sequences inside dictionaries when not running verbose mode. From 770d8ba2ba1b0253923075883ffa85ca2bb0306a Mon Sep 17 00:00:00 2001 From: Reagan Date: Thu, 15 Aug 2024 23:04:42 -0700 Subject: [PATCH 5/7] mypy --- src/_pytest/assertion/util.py | 5 +++-- testing/test_assertion.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 21a7912e17a..97d2d6b9251 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -14,6 +14,7 @@ from typing import Mapping from typing import Protocol from typing import Sequence +from typing import Union from unicodedata import normalize from _pytest import outcomes @@ -354,7 +355,7 @@ def _compare_eq_iterable( def _compare_eq_sequence( left: Sequence[Any], right: Sequence[Any], - highlighter: _HighlightFunc, + highlighter: Union[_HighlightFunc, Callable[[str], str]], verbose: int = 0, ) -> list[str]: comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes) @@ -508,7 +509,7 @@ def _compare_eq_dict( expl = _compare_eq_sequence( left_val, right_val, lambda item: item, verbose ) - expl = str("".join(expl)) + expl = "".join(expl) explanation += [highlighter(saferepr({k: expl}))] continue diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 5a22bc0a007..a6b563780eb 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -745,7 +745,7 @@ def test_dict_sequence_items(self) -> None: value1 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 1]} value2 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 2]} lines = callequal(value1, value2, verbose=0) - assert "{'asdf': 'At index 7 diff: 1 != 2'}" in "".join(lines) + assert "{'asdf': 'At index 7 diff: 1 != 2'}" in lines def test_sequence_different_items(self) -> None: lines = callequal((1, 2), (3, 4, 5), verbose=2) From a1b0239fb5e3cde790277e112a286f4d319961b2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Aug 2024 06:05:30 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 97d2d6b9251..d54f91bb7ac 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -14,7 +14,6 @@ from typing import Mapping from typing import Protocol from typing import Sequence -from typing import Union from unicodedata import normalize from _pytest import outcomes @@ -355,7 +354,7 @@ def _compare_eq_iterable( def _compare_eq_sequence( left: Sequence[Any], right: Sequence[Any], - highlighter: Union[_HighlightFunc, Callable[[str], str]], + highlighter: _HighlightFunc | Callable[[str], str], verbose: int = 0, ) -> list[str]: comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes) From 41c48fb497b7265a991343ff531a78d4404eca9c Mon Sep 17 00:00:00 2001 From: Reagan Date: Thu, 15 Aug 2024 23:37:06 -0700 Subject: [PATCH 7/7] mypy + comment clean --- src/_pytest/assertion/util.py | 4 +--- testing/test_assertion.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index d54f91bb7ac..cf915c0796c 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -504,12 +504,10 @@ def _compare_eq_dict( left_val = left[k] right_val = right[k] if issequence(left_val) and issequence(right_val): - # explanation.append(saferepr("BLAHBLAH: string")) expl = _compare_eq_sequence( left_val, right_val, lambda item: item, verbose ) - expl = "".join(expl) - explanation += [highlighter(saferepr({k: expl}))] + explanation += [highlighter(saferepr({k: "".join(expl)}))] continue explanation += [ diff --git a/testing/test_assertion.py b/testing/test_assertion.py index a6b563780eb..814a7730933 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -745,7 +745,7 @@ def test_dict_sequence_items(self) -> None: value1 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 1]} value2 = {"asdf": [1, 1, 1, 1, 1, 1, 1, 2]} lines = callequal(value1, value2, verbose=0) - assert "{'asdf': 'At index 7 diff: 1 != 2'}" in lines + assert lines and "{'asdf': 'At index 7 diff: 1 != 2'}" in lines def test_sequence_different_items(self) -> None: lines = callequal((1, 2), (3, 4, 5), verbose=2)