Skip to content

Commit 144ef9c

Browse files
ale-molinaripre-commit-ci[bot]poyea
authored
Fix type hints in sorts/tim_sort.py, relates to #14457 (#14474)
* Add type hints to tim_sort.py, relates to #14457 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix ruff PYI041 error: use float instead of int | float * Fix mypy error: support str and tuple inputs as defined in doctests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix ruff E501: wrap binary_search parameters to respect 88 char limit * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor generics to use Python 3.12 type parameter syntax (PEP 695) * Use Any from typing to resolve mypy list unpacking bugs * [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: John Law <johnlaw.po@gmail.com>
1 parent abf7168 commit 144ef9c

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

sorts/tim_sort.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def binary_search(lst, item, start, end):
1+
from typing import Any
2+
3+
4+
def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int:
25
if start == end:
36
return start if lst[start] > item else start + 1
47
if start > end:
@@ -13,7 +16,7 @@ def binary_search(lst, item, start, end):
1316
return mid
1417

1518

16-
def insertion_sort(lst):
19+
def insertion_sort(lst: list[Any]) -> list[Any]:
1720
length = len(lst)
1821

1922
for index in range(1, length):
@@ -24,7 +27,7 @@ def insertion_sort(lst):
2427
return lst
2528

2629

27-
def merge(left, right):
30+
def merge(left: list[Any], right: list[Any]) -> list[Any]:
2831
if not left:
2932
return right
3033

@@ -37,7 +40,7 @@ def merge(left, right):
3740
return [right[0], *merge(left, right[1:])]
3841

3942

40-
def tim_sort(lst):
43+
def tim_sort(lst: list[Any] | tuple[Any, ...] | str) -> list[Any]:
4144
"""
4245
>>> tim_sort("Python")
4346
['P', 'h', 'n', 'o', 't', 'y']
@@ -53,7 +56,7 @@ def tim_sort(lst):
5356
length = len(lst)
5457
runs, sorted_runs = [], []
5558
new_run = [lst[0]]
56-
sorted_array = []
59+
sorted_array: list[Any] = []
5760
i = 1
5861
while i < length:
5962
if lst[i] < lst[i - 1]:

0 commit comments

Comments
 (0)