Repository navigation
fix(sorts): support less-than-only items in iterative merge sort - #15690
Open
parijat1222q wants to merge 1 commit into
Open
parijat1222q wants to merge 1 commit into
parijat1222q wants to merge 1 commit into
Conversation
mindaugl
approved these changes
Nov 4, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change
Comparableinsorts/iterative_merge_sort.pyrequires only__lt__, butmergeuses<=. Values that implement the declared protocol can therefore be sorted by Python'ssortedand the siblingmerge_sort, yet fail here withTypeError.Select from the right when
right[0] < left[0]; otherwise select from the left. This uses only the declared operation and keeps the left-first ordering for equal keys. It does not change the existingpop(0)implementation or other algorithms.Related contract: #15234 and #15413. This is a runtime fix, not another type-annotation change. No closing keyword is used for the umbrella issue.
Reproduction
On
masterat574d100b7181956e9c8384552fd078165e4cc326, with Python 3.15t:Before:
TypeError: '<=' not supported between instances of 'Item' and 'Item'.After:
[1, 2].Validation
The current contribution guide asks not to change code and doctests in the same PR, so this changes one algorithm source file only. Existing doctests are unchanged.
{0, 1, 2}, using objects that define only__lt__; compares object identity/order withsorted(including stable ties), checks input preservation, exercisesmergedirectly, and retainsTypeErrorfor[1, "a"].uv run --python 3.15t --no-project --with pytest python -m pytest -q -o addopts= tests/test_sorts.py sorts/iterative_merge_sort.py --doctest-modules: 456 passed.ruff check sorts/iterative_merge_sort.pyandruff format --check sorts/iterative_merge_sort.py: passed.Checklist