Skip to content

Commit b8c5717

Browse files
Fix: Preserve dict insertion order in assertion output
Remove sorted() calls in PrettyPrinter to preserve dict insertion order. Since Python 3.7+, dicts maintain insertion order. Sorting keys alphabetically in assertion failures is confusing and doesn't match how Python naturally displays dicts. Changes: - _pprint_dict: Remove sorted() on object.items() - _safe_repr: Remove sorted() on dict.items() iteration Fixes #13503
1 parent d794da3 commit b8c5717

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

src/_pytest/_io/pprint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def _pprint_dict(
162162
) -> None:
163163
write = stream.write
164164
write("{")
165-
items = sorted(object.items(), key=_safe_tuple)
165+
items = object.items()
166166
self._format_dict_items(items, stream, indent, allowance, context, level)
167167
write("}")
168168

@@ -608,7 +608,7 @@ def _safe_repr(
608608
components: list[str] = []
609609
append = components.append
610610
level += 1
611-
for k, v in sorted(object.items(), key=_safe_tuple):
611+
for k, v in object.items():
612612
krepr = self._safe_repr(k, context, maxlevels, level)
613613
vrepr = self._safe_repr(v, context, maxlevels, level)
614614
append(f"{krepr}: {vrepr}")

0 commit comments

Comments
 (0)