Skip to content

Conversation

@anuranjansb
Copy link

What does this PR do?

This PR fixes an issue in pytest’s assertion comparison output where dictionary
insertion order is not preserved in verbose assertion diffs.

When comparing dictionaries with extra keys, the current output may inject the
extra key into the structured diff block, even though it belongs only to the
right-hand side. This leads to confusing output and breaks the expectation that
dict insertion order is respected.

This PR ensures that:

  • Extra keys are reported separately (e.g. Right contains 1 more item)
  • Extra keys are not injected into the structured “Full diff” block
  • Dictionary insertion order is preserved in assertion output

Why is this needed?

This is a regression highlighted in issue #14079.

Python dictionaries preserve insertion order, but pytest’s assertion diff output
did not consistently reflect this, especially for nested dictionaries combined
with wrapped output. This resulted in misleading diffs such as extra keys
appearing inside the left-hand structure.


What changes were made?

  • Updated _compare_eq_dict logic in src/_pytest/assertion/util.py to ensure
    extra keys are displayed only in the appropriate section.
  • Added a regression test to testing/test_assertion.py that fails under the
    current behavior and passes with this fix.

How was this tested?

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jan 4, 2026
@nicoddemus
Copy link
Member

Thanks @anuranjansb, but this has the same issues as #14066. Please take a look at that PR's comments, specially the test that should be done.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @anuranjansb, but this has the same issues as #14066. Please take a look at that PR's comments, specially the test that should be done.

@anuranjansb
Copy link
Author

Thanks for pointing that out!

I’ll review the comments on #14066 and adjust the regression test accordingly.
I see now that the test should assert the behavioral guarantees (extra keys
being reported separately and not injected into the structured diff), rather
than matching the full formatted output.

I’ll update the test and follow up shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants