Skip to content

gh-145546: unittest.util: fix sorted_list_difference tail deduplication#145547

Open
stefanzetzsche wants to merge 8 commits intopython:mainfrom
stefanzetzsche:fix/unittest_util
Open

gh-145546: unittest.util: fix sorted_list_difference tail deduplication#145547
stefanzetzsche wants to merge 8 commits intopython:mainfrom
stefanzetzsche:fix/unittest_util

Conversation

@stefanzetzsche
Copy link

@stefanzetzsche stefanzetzsche commented Mar 5, 2026

Problem

sorted_list_difference doesn't deduplicate remaining elements when one list is exhausted. The except IndexError fallback uses extend(expected[i:]) / extend(actual[j:]) which includes duplicates.

Reproducer

from unittest.util import sorted_list_difference
print(sorted_list_difference([], [0, 0]))  # ([], [0, 0]) — expected ([], [0])

Fix

Replace the raw extend(expected[i:]) / extend(actual[j:]) in the except IndexError fallback with a helper that removes consecutive duplicates. The helper only uses equality comparison (not hashing), matching the main loop's approach and preserving support for unhashable types like lists.

Tests

  • Tail deduplication with ints (empty list vs duplicates, mid-way exhaustion)
  • String inputs with duplicated tails
  • Unhashable types (lists of lists) to verify no hashing requirement is introduced

sorted_list_difference failed to deduplicate remaining elements when one
list was exhausted, causing duplicate values in the result.

Fix uses dict.fromkeys() to deduplicate before extending.

sim: https://taskei.amazon.dev/tasks/TODO
…ication

Exercise the except-IndexError path where one list is exhausted
and the remaining tail of the other list contains duplicates.
These cases were previously untested and would have passed even
without the fix in the preceding commit.
@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@stefanzetzsche stefanzetzsche marked this pull request as ready for review March 5, 2026 14:14
Sphinx cannot resolve :func: references to unittest.util since it is
not part of the public documented API. Use double backticks instead.
dict.fromkeys requires hashable elements, which narrows the input
contract. Replace with a simple consecutive-duplicate removal that
only uses equality comparison, matching the main loop's approach.

Add test cases for strings and unhashable types (lists).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants