Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions changelog/13503.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preserved insertion order for extra dictionary items in assertion failure output.
Display dictionary differences in assertion failures using the original key insertion order instead of sorted order.
4 changes: 2 additions & 2 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def _compare_eq_dict(
+ " != "
+ highlighter(saferepr({k: right[k]}))
]
extra_left = set_left - set_right
extra_left = [k for k in left if k not in right]
len_extra_left = len(extra_left)
if len_extra_left:
explanation.append(
Expand All @@ -529,7 +529,7 @@ def _compare_eq_dict(
explanation.extend(
highlighter(pprint.pformat({k: left[k] for k in extra_left})).splitlines()
)
extra_right = set_right - set_left
extra_right = [k for k in right if k not in left]
len_extra_right = len(extra_right)
if len_extra_right:
explanation.append(
Expand Down
33 changes: 33 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from collections.abc import MutableSequence
import re
import sys
import textwrap
from typing import Any
Expand Down Expand Up @@ -2198,3 +2199,35 @@ def test_vvv():
]
)
result.stdout.no_fnmatch_line(expected_non_vvv_arg_line)


def test_dict_extra_items_preserve_insertion_order(pytester):
pytester.makepyfile(
test_order="""
def test_order():
a = {
"first": 1,
"second": 2,
}
assert a == {}
"""
)

result = pytester.runpytest()

stdout = result.stdout.str()
stdout = re.sub(r"\x1b\[[0-9;]*m", "", stdout)

assert "Left contains 2 more items:" in stdout

dict_line = next(
line
for line in stdout.splitlines()
if "{" in line and "}" in line and "first" in line
)

first = dict_line.find("first")
second = dict_line.find("second")

assert first != -1 and second != -1
assert first < second