Skip to content

Commit 6619743

Browse files
committed
Use Any from typing to resolve mypy list unpacking bugs
1 parent f41a6b2 commit 6619743

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

sorts/tim_sort.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
def binary_search[T: (int, float, str)](
2-
lst: list[T],
3-
item: T,
4-
start: int,
5-
end: int,
6-
) -> int:
1+
from typing import Any
2+
3+
def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int:
74
if start == end:
85
return start if lst[start] > item else start + 1
96
if start > end:
@@ -18,7 +15,7 @@ def binary_search[T: (int, float, str)](
1815
return mid
1916

2017

21-
def insertion_sort[T: (int, float, str)](lst: list[T]) -> list[T]:
18+
def insertion_sort(lst: list[Any]) -> list[Any]:
2219
length = len(lst)
2320

2421
for index in range(1, length):
@@ -29,7 +26,7 @@ def insertion_sort[T: (int, float, str)](lst: list[T]) -> list[T]:
2926
return lst
3027

3128

32-
def merge[T: (int, float, str)](left: list[T], right: list[T]) -> list[T]:
29+
def merge(left: list[Any], right: list[Any]) -> list[Any]:
3330
if not left:
3431
return right
3532

@@ -42,9 +39,7 @@ def merge[T: (int, float, str)](left: list[T], right: list[T]) -> list[T]:
4239
return [right[0], *merge(left, right[1:])]
4340

4441

45-
def tim_sort[T: (int, float, str)](
46-
lst: list[T] | tuple[T, ...] | str,
47-
) -> list[T] | list[str]:
42+
def tim_sort(lst: list[Any] | tuple[Any, ...] | str) -> list[Any]:
4843
"""
4944
>>> tim_sort("Python")
5045
['P', 'h', 'n', 'o', 't', 'y']
@@ -60,7 +55,7 @@ def tim_sort[T: (int, float, str)](
6055
length = len(lst)
6156
runs, sorted_runs = [], []
6257
new_run = [lst[0]]
63-
sorted_array: list[T] = []
58+
sorted_array: list[Any] = []
6459
i = 1
6560
while i < length:
6661
if lst[i] < lst[i - 1]:

0 commit comments

Comments
 (0)