fix(sorts): make power_sort generic over Comparable items - #15438
Open
wanjinhao1 wants to merge 2 commits into
Open
wanjinhao1 wants to merge 2 commits into
wanjinhao1 wants to merge 2 commits into
Conversation
- Add a Comparable protocol and TypeVar bound (PEP 695) so power_sort accepts any iterable of mutually comparable items instead of bare list - Rewrite run detection and merge comparisons to use only __lt__, which is all the protocol guarantees - Add doctest + shared test battery coverage, including the mixed-type TypeError failure mode
for more information, see https://pre-commit.ci
wanjinhao1
force-pushed
the
agent/fix-15234-power-sort-comparable
branch
from
September 25, 2026 14:43
9e3b140 to
03e45c0
Compare
Member
|
@priya-sundaram-dev, please review. I sense that other functions need the |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address the iterable/list contract and Comparable typing issues before approval.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Generalizes power_sort to support generic comparable items and iterable inputs, with expanded shared test coverage.
Changes:
- Adds
Comparabletyping and<-only comparisons. - Registers
power_sortin shared sort tests. - Adds mixed-type rejection coverage.
| File | Summary |
|---|---|
sorts/power_sort.py |
Updates typing, comparisons, and doctests. Materialize iterables before length checks, return a list, tighten the protocol operand type, and update documentation. |
tests/test_sorts.py |
Adds shared sorting and non-comparable rejection coverage. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+205
to
+206
| def power_sort[T: Comparable]( | ||
| collection: Iterable[T], |
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
Checklist
Part of #15234 (does not close the tracking issue).
What
Comparableprotocol (__lt__only) and a PEP 695TypeVarbound sopower_sortaccepts any iterable of mutually comparable items (Iterable[T]→list[T]) instead of a barelist. Thekeypath staysCallable[[T], Any]per the audit note in the tracker discussion.<(the protocol guarantees nothing else): the ascending-run check>=becomesnot (... < ...)and the merge tieleft <= rightbecomesnot (right < left), which preserves stability for total orders.power_sort([1, "a"])→TypeError).power_sortin the sharedSORTSbattery and the non-comparable rejection list intests/test_sorts.py.Why
power_sort.pyis one of the remaining unchecked comparison sorts in the #15234 checklist, with no open PR and no assignee.Validation
python3 -m doctest sorts/power_sort.py— all doctests pass (including the newTypeErrorcase)python3 -m pytest tests/test_sorts.py -q— 395 passed (includes power_sort across the full shared CASES battery and the[1, "a"]rejection test)Note: one algorithm per PR per the tracker guidance. This PR was completed with AI assistance under the supervision of the account owner (@wanjinhao1) — happy to address any review feedback.