sorts: make odd_even_sort generic over any comparable type (Part of #15234) - #15378
aayushgupta7725 wants to merge 7 commits into
Conversation
…heAlgorithms#15234) Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test. Part of TheAlgorithms#15234
for more information, see https://pre-commit.ci
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
Thanks @aayushgupta7725 — this reads cleanly and the added doctests (str, float, and the mixed-type TypeError) are a nice touch. One small simplification:
The module-level Comparable/TypeVar boilerplate is now redundant with the PEP 695 syntax you used on the signature. def odd_even_sort[T: Comparable](...) introduces its own scoped T, so the top-level T = TypeVar("T", bound="Comparable") is dead — nothing references it. You can drop that line and the TypeVar import (keep Protocol, and Any is still used inside the protocol):
from collections.abc import MutableSequence
from typing import Any, Protocol
class Comparable(Protocol):
def __gt__(self, other: Any, /) -> bool: ...
def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:Tiny consistency nit while you're there: the algorithm branches on collection[i] > collection[i + 1], so the protocol bound is more accurate as __gt__ than __lt__ (matches the operator the function actually relies on). Not a CI blocker — ty is happy either way — just makes the bound say what the code does.
Otherwise LGTM. 👍
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
LGTM — this cleanly mirrors the pattern ( protocol + PEP 695 on a ), and registering it in / the non-comparable rejection test is exactly right. I ran the four new doctests locally and they all pass, including the → case.
One optional nit, no need to block on it: uses (unquoted, since the class is already defined above), whereas here it is . Both work; dropping the quotes would keep it byte-for-byte consistent with the reference file. Nice work.
|
(Reposting my review note — GitHub ate the code spans in the previous one.) Approving: this mirrors the One optional nit (don't block on it): |
…upta7725/Python into odd-even-sort-comparable
|
Thanks for the review! I've pushed both requested changes:
Also fixed a Let me know if anything else needs adjusting! |
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
Both changes look great, thanks for the quick turnaround.
- Dropping the module-level
TypeVarin favor of the inline[T: Comparable]scoping is the right call — matches theinsertion_sort.pypattern exactly. - Switching the
Comparableprotocol to__gt__now correctly mirrors the>the algorithm actually uses.
I re-ran the 7 doctests on the latest commit (5d21113) locally — all pass, including the [1, "a"] → TypeError case. LGTM. 👍
Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test.
Part of #15234
@priya-sundaram-dev, this is ready whenever you get a chance to review.
Describe your change
Checklist