Skip to content
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

Improve AssertionError error message for sequences inside dictionaries #12717

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]],
reaganjlee marked this conversation as resolved.
Show resolved Hide resolved
verbose: int = 0,
) -> list[str]:
comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down