GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Preserve insertion order for extra dict items in assertion output by RafiB982 · Pull Request #14066 · pytest-dev/pytest

Preserve insertion order for extra dict items in assertion output #14066

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.

Sign up for GitHub

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

Closed
RafiB982 wants to merge 4 commits into pytest-dev:main from RafiB982:preserve-dict-insertion-order

Uh oh!

There was an error while loading. Please reload this page.

Closed

Preserve insertion order for extra dict items in assertion output #14066

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
0262fd3
Preserve insertion order for extra dict items in assertion output
RafiB982 Dec 26, 2025
5bb95f2
Fix changelog entry for #13503
RafiB982 Dec 31, 2025
69ce953
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 31, 2025
7e7f100
Fix import order in test_assertion
RafiB982 Dec 31, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 changelog/13503.bugfix.rst
Show comments
View file
Open in desktop
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 4 src/_pytest/assertion/util.py
Show comments
View file
Open in desktop
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 33 testing/test_assertion.py
Show comments
View file
Open in desktop
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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.