Skip to content

fix: preserve nested mapping values in assertion diffs - #15222

Closed
rudra337-lgtm wants to merge 2 commits into
pytest-dev:mainfrom
rudra337-lgtm:fix/11980-dict-list-diff
Closed

rudra337-lgtm wants to merge 2 commits into
pytest-dev:mainfrom
rudra337-lgtm:fix/11980-dict-list-diff

Conversation

@rudra337-lgtm

Copy link
Copy Markdown

What was wrong

When an assertion compared mappings whose differing values were lists, pytest used saferepr for the entire one-item mapping. Its nested repr truncation could hide the actual differing list element, even for a short eight-item list.

What changed

Render differing mapping entries with pprint.pformat, retaining the existing safe-repr fallback when formatting user objects raises. Added a regression test for the reported nested-list case.

Verification

  • Baseline reproduction: the eight-item lists were shown as [1, 1, 1, 1, 1, 1, ...] on both sides.
  • Focused assertion tests: 2 passed, 207 deselected.
  • git diff --check passed.

Fixes #11980

@ege-arhan ege-arhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When differing mapping values wrap across multiple lines (e.g. nested lists or dictionaries with 10+ items exceeding pprint width), highlighter(left_repr) + " != " + highlighter(right_repr) glues != directly onto the last line of left_repr and embeds unhandled \n characters:

d1 = {"key": [f"item_{i}" for i in range(15)]}
d2 = {"key": [f"item_{i}" for i in range(14)] + ["diff"]}
# asserting d1 == d2 formats as:
# {key: [item_0,
#          ...,
#          item_14]} != {key: [item_0,
#          ...,
#          diff]}

Here the closing bracket of the left mapping shares a line with != and the first line of the right mapping, and the raw newlines bypass terminal reporter line prefixing/indentation. If multiline pretty-printing is introduced here, differing items should either split lines cleanly across separate blocks or preserve single-line rendering.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssertionErrors with differing items in dict of lists do not display properly when not running in verbose mode

3 participants