From a3d5b3bb1f248bdad460f1d50204593ab3405b6d Mon Sep 17 00:00:00 2001 From: 5h4d0wn1k Date: Tue, 22 Sep 2026 21:50:20 +0530 Subject: [PATCH 1/3] feat(sorts): make reversort generic over comparable items Part of #15234 - Replace list[Any] with a Comparable-bounded TypeVar so reversort and reversort_cost sort any mutually comparable items, not just ints - Add TypeError doctests confirming mixed non-comparable input raises - Register reversort in the shared test battery covering str/float/ dataclass/NamedTuple cases and the non-comparable rejection path --- sorts/reversort.py | 21 ++++++++++++++++++--- tests/test_sorts.py | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/sorts/reversort.py b/sorts/reversort.py index c600d3fda0d7..0271e2f20ea8 100644 --- a/sorts/reversort.py +++ b/sorts/reversort.py @@ -17,10 +17,17 @@ python reversort.py """ -from typing import Any +from typing import Any, Protocol, TypeVar -def reversort(collection: list[Any]) -> list[Any]: +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def reversort(collection: list[T]) -> list[T]: """ Sort a list using the Reversort algorithm. @@ -78,6 +85,10 @@ def reversort(collection: list[Any]) -> list[Any]: >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100) >>> reversort(collection_arg) == sorted(collection_arg) True + >>> reversort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ arr = collection[:] # Create a copy to avoid modifying the original n = len(arr) @@ -96,7 +107,7 @@ def reversort(collection: list[Any]) -> list[Any]: return arr -def reversort_cost(collection: list[Any]) -> int: +def reversort_cost(collection: list[T]) -> int: """ Calculate the cost of sorting using Reversort. @@ -119,6 +130,10 @@ def reversort_cost(collection: list[Any]) -> int: 0 >>> reversort_cost([]) 0 + >>> reversort_cost([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' """ arr = collection[:] # Create a copy to avoid modifying the original n = len(arr) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index e3e91838a792..84e4f64812e7 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -40,6 +40,7 @@ from sorts.patience_sort import patience_sort from sorts.quick_sort import quick_sort from sorts.recursive_insertion_sort import rec_insertion_sort +from sorts.reversort import reversort from sorts.selection_sort import selection_sort from sorts.shell_sort import shell_sort from sorts.shrink_shell_sort import shell_sort as shrink_shell_sort @@ -73,6 +74,7 @@ def test_heap_sort() -> None: pancake_sort, patience_sort, quick_sort, + reversort, selection_sort, shell_sort, shrink_shell_sort, @@ -140,6 +142,7 @@ def test_rec_insertion_sort(case) -> None: merge_sort, odd_even_sort, pancake_sort, + reversort, selection_sort, shrink_shell_sort, strand_sort, From 65d7ba5bdc9c61d6f6a5b997a73852e233fef64b Mon Sep 17 00:00:00 2001 From: 5h4d0wn1k Date: Tue, 22 Sep 2026 22:23:10 +0530 Subject: [PATCH 2/3] style(sorts): use PEP 695 type parameters per repo convention ruff UP047 requires the sanctioned [T: Comparable] signature form used by the reference insertion_sort.py. Keep the module-level TypeVar as in the reference. --- sorts/reversort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/reversort.py b/sorts/reversort.py index 0271e2f20ea8..4a900ef08610 100644 --- a/sorts/reversort.py +++ b/sorts/reversort.py @@ -27,7 +27,7 @@ def __lt__(self, other: Any, /) -> bool: ... T = TypeVar("T", bound=Comparable) -def reversort(collection: list[T]) -> list[T]: +def reversort[T: Comparable](collection: list[T]) -> list[T]: """ Sort a list using the Reversort algorithm. @@ -107,7 +107,7 @@ def reversort(collection: list[T]) -> list[T]: return arr -def reversort_cost(collection: list[T]) -> int: +def reversort_cost[T: Comparable](collection: list[T]) -> int: """ Calculate the cost of sorting using Reversort. From 1e941882c9ef72c092aa9979e8991424eac8a313 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 22 Sep 2026 22:58:37 +0200 Subject: [PATCH 3/3] Apply batched suggestions from code review Co-authored-by: Christian Clauss --- sorts/reversort.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sorts/reversort.py b/sorts/reversort.py index 4a900ef08610..a2eb4a29a31f 100644 --- a/sorts/reversort.py +++ b/sorts/reversort.py @@ -17,16 +17,13 @@ python reversort.py """ -from typing import Any, Protocol, TypeVar +from typing import Any, Protocol class Comparable(Protocol): def __lt__(self, other: Any, /) -> bool: ... -T = TypeVar("T", bound=Comparable) - - def reversort[T: Comparable](collection: list[T]) -> list[T]: """ Sort a list using the Reversort algorithm.