Skip to content

Show correct extra item in sequence comparison diffs - #14906

Open
SemTiOne wants to merge 27 commits into
pytest-dev:mainfrom
SemTiOne:fix/13652-misleading-extra-item-message
Open

SemTiOne wants to merge 27 commits into
pytest-dev:mainfrom
SemTiOne:fix/13652-misleading-extra-item-message

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Prior art: #14286.

Pytest reported the wrong extra item when comparing sequences of different lengths. It printed the item past the end of the shorter side. The real extra item sat at the first differing index.
Before:

E     assert [1, 2, 3] == [1, 2, 0, 3]
E       At index 2 diff: 3 != 0
E       Right contains one more item: 3   (wrong: extra item is 0)

After:

E     Right contains one more item: 0

The fix finds a single inserted item by checking whether the tails after the first differing index align, offset by one. It corrects only the single-extra-item case. Multi-item length differences still use the previous "first extra item" message.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Closes Misleading message "Right contains one more item" #13652
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Add yourself to AUTHORS in alphabetical order.

Co-authored-by: Claude <noreply@anthropic.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 19, 2026
@SemTiOne
SemTiOne marked this pull request as ready for review August 19, 2026 06:30
SemTiOne and others added 18 commits August 22, 2026 07:02
Added fallback test cases for extra items in lists.
Slice comparison may raise or return non-bools for custom Sequences; fall back to the first-extra-item message instead of degrading the whole explanation. No known in-tree trigger.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>

@OKAN-SOFTWARE OKAN-SOFTWARE 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.

Reviewed commit 886f14b.

The updated message correctly handles cases where a single element was inserted in the middle of an otherwise matching sequence.

However, the new found_diff and len_diff == 1 condition does not distinguish a genuine insertion from a combination of differing values and unequal lengths.

For example:

left = [1, 2, 9]
right = [1, 3]

The new logic reports:

At index 1 diff: 2 != 3
Left contains one more item: 2

But removing 2 produces [1, 9], which still differs from the right-hand sequence. There is no uniquely identifiable inserted element in this example.

The PR description mentions checking whether the remaining elements align after the first difference, but the current implementation only checks found_diff and the length difference.

Could we verify suffix alignment before identifying the first differing element as the extra item?

For example:

  • When the left side is longer, check whether left[i + 1:] == right[i:].
  • When the right side is longer, check whether right[i + 1:] == left[i:].

If the remaining elements don't align, retaining a more general length-difference message would avoid identifying an unsupported insertion.

A regression test containing both a length difference and unrelated mismatched values would help cover this distinction.

The focused selection logic was checked with the example above. GitHub's test workflow passes; I have not independently run the complete pytest suite.

@SemTiOne

SemTiOne commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Done. Thanks for your review.

@SemTiOne
SemTiOne force-pushed the fix/13652-misleading-extra-item-message branch from bee4ae0 to 5385e1a Compare November 5, 2026 15:57
@OKAN-SOFTWARE

Copy link
Copy Markdown

Thanks for addressing my previous feedback. The updated suffix-alignment check resolves the issue I identified, and the added regression tests cover the problematic cases.

The latest CI checks are passing. Looks good from my side!

This branch has not been deployed

No deployments
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.

Misleading message "Right contains one more item"

2 participants