From 80e645c63a298d7e5ec15d2698ba28ff9315a796 Mon Sep 17 00:00:00 2001 From: HasanGenis Date: Wed, 23 Sep 2026 14:11:45 +0300 Subject: [PATCH 1/2] feat(sorts): make recursive quick sort generic over comparable items Add a Comparable Protocol + TypeVar bound to recursive_quick_sort's quick_sort() so it is typed list[T] instead of a bare list, matching the pattern in insertion_sort.py. Adds a doctest asserting TypeError on non-comparable mixed-type input and registers it (aliased to recursive_quick_sort to avoid colliding with sorts.quick_sort.quick_sort) in the shared test battery and in test_sort_rejects_non_comparable_items. Part of #15234 --- sorts/recursive_quick_sort.py | 17 ++++++++++++++++- tests/test_sorts.py | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sorts/recursive_quick_sort.py b/sorts/recursive_quick_sort.py index c29009aca673..62808e288f52 100644 --- a/sorts/recursive_quick_sort.py +++ b/sorts/recursive_quick_sort.py @@ -1,10 +1,25 @@ -def quick_sort(data: list) -> list: +from typing import Any, Protocol, TypeVar + + +class Comparable(Protocol): + def __lt__(self, other: Any, /) -> bool: ... + + +T = TypeVar("T", bound=Comparable) + + +def quick_sort[T: Comparable](data: list[T]) -> list[T]: """ >>> for data in ([2, 1, 0], [2.2, 1.1, 0], "quick_sort"): ... quick_sort(data) == sorted(data) True True True + + >>> quick_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'str' and 'int' """ if len(data) <= 1: return data diff --git a/tests/test_sorts.py b/tests/test_sorts.py index d18be1c22b83..44d6a49a95f3 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -43,6 +43,7 @@ from sorts.quick_sort import quick_sort from sorts.recursive_insertion_sort import rec_insertion_sort from sorts.recursive_mergesort_array import merge +from sorts.recursive_quick_sort import quick_sort as recursive_quick_sort from sorts.reversort import reversort from sorts.selection_sort import selection_sort from sorts.shell_sort import shell_sort @@ -79,6 +80,7 @@ def test_heap_sort() -> None: pancake_sort, patience_sort, quick_sort, + recursive_quick_sort, reversort, selection_sort, shell_sort, @@ -149,6 +151,7 @@ def test_rec_insertion_sort(case) -> None: odd_even_sort, odd_even_transposition, pancake_sort, + recursive_quick_sort, reversort, selection_sort, shrink_shell_sort, From 792887f515490b8c557f4fc73c2264b5f420338a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Sep 2026 16:39:16 +0200 Subject: [PATCH 2/2] Apply suggestion from @cclauss --- sorts/recursive_quick_sort.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sorts/recursive_quick_sort.py b/sorts/recursive_quick_sort.py index 62808e288f52..d71e79fd6450 100644 --- a/sorts/recursive_quick_sort.py +++ b/sorts/recursive_quick_sort.py @@ -1,13 +1,10 @@ -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 quick_sort[T: Comparable](data: list[T]) -> list[T]: """ >>> for data in ([2, 1, 0], [2.2, 1.1, 0], "quick_sort"):