Skip to content

Commit ae429ad

Browse files
Silverbullets1pre-commit-ci[bot]cclauss
authored
typing: bind recursive insertion sort to Comparable protocol (#15472)
* typing: bind recursive insertion sort to Comparable protocol - insert_next was missing its TypeVar bound (Pyright: '<' operator invalid on T) - Protocol __lt__ takes object, not Any; order check now relies on __lt__ only - doctests: floats+ints mix, mixed [1, 'a'] raises TypeError Part of #15234 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 4817902 commit ae429ad

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎ciphers/des_ecb.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,18 @@ def decrypt(key: str, cipher_text: str) -> str:
544544
print(f"Generated key: {Des.generate_key()}")
545545
case "e":
546546
key = input("Enter the key: ").strip()
547-
if len(key) != 16 and not all(char in "0123456789abcdef" for char in key):
547+
if len(key) != 16 and not all(
548+
char in "0123456789abcdef" for char in key
549+
):
548550
print("Invalid key. Please try again.")
549551
continue
550552
input_string = input("Enter the string to encrypt: ").strip()
551553
print(f"Encrypted string: {Des.encrypt(key, input_string)}")
552554
case "d":
553555
key = input("Enter the key: ").strip()
554-
if len(key) != 16 and not all(char in "0123456789abcdef" for char in key):
556+
if len(key) != 16 and not all(
557+
char in "0123456789abcdef" for char in key
558+
):
555559
print("Invalid key. Please try again.")
556560
continue
557561
cipher_text = input("Enter the cipher text to decrypt: ").strip()

‎sorts/recursive_insertion_sort.py‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
from __future__ import annotations
66

77
from collections.abc import MutableSequence
8-
from typing import Any, Protocol, TypeVar
8+
from typing import Protocol, TypeVar
99

1010

1111
class Comparable(Protocol):
12-
def __lt__(self, other: Any, /) -> bool: ...
12+
def __lt__(self, other: object, /) -> bool: ...
1313

1414

1515
T = TypeVar("T", bound=Comparable)
1616

1717

18-
def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None:
18+
def rec_insertion_sort[T: Comparable](collection: MutableSequence[T], n: int) -> None:
1919
"""
2020
Given a collection of comparable elements and its length, sorts the
2121
collection in place in ascending order.
@@ -42,6 +42,17 @@ def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None:
4242
>>> rec_insertion_sort(col, len(col))
4343
>>> col
4444
['a', 'b', 'c', 'd', 'e']
45+
46+
>>> col = [2.5, -1, 0.0, 1]
47+
>>> rec_insertion_sort(col, len(col))
48+
>>> col
49+
[-1, 0.0, 1, 2.5]
50+
51+
>>> col = [1, "a"]
52+
>>> rec_insertion_sort(col, len(col))
53+
Traceback (most recent call last):
54+
...
55+
TypeError: '<' not supported between instances of 'str' and 'int'
4556
"""
4657
# Checks if the entire collection has been sorted
4758
if len(collection) <= 1 or n <= 1:
@@ -51,7 +62,7 @@ def rec_insertion_sort[T](collection: MutableSequence[T], n: int) -> None:
5162
rec_insertion_sort(collection, n - 1)
5263

5364

54-
def insert_next[T](collection: MutableSequence[T], index: int) -> None:
65+
def insert_next[T: Comparable](collection: MutableSequence[T], index: int) -> None:
5566
"""
5667
Inserts the '(index-1)th' element into place
5768
@@ -71,7 +82,8 @@ def insert_next[T](collection: MutableSequence[T], index: int) -> None:
7182
[]
7283
"""
7384
# Checks order between adjacent elements
74-
if index >= len(collection) or collection[index - 1] <= collection[index]:
85+
# Only relies on ``__lt__`` so any Comparable works
86+
if index >= len(collection) or not collection[index] < collection[index - 1]:
7587
return
7688

7789
# Swaps adjacent elements since they are not in ascending order

0 commit comments

Comments
 (0)