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>
This commit is contained in:
Alessandro Molinari
2026-05-18 23:14:42 +01:00
committed by GitHub
co-authored by pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> John Law
parent abf7168f57
commit 144ef9c022
+8 -5
View File
@@ -1,4 +1,7 @@
def binary_search(lst, item, start, end):
from typing import Any
def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int:
if start == end:
return start if lst[start] > item else start + 1
if start > end:
@@ -13,7 +16,7 @@ def binary_search(lst, item, start, end):
return mid
def insertion_sort(lst):
def insertion_sort(lst: list[Any]) -> list[Any]:
length = len(lst)
for index in range(1, length):
@@ -24,7 +27,7 @@ def insertion_sort(lst):
return lst
def merge(left, right):
def merge(left: list[Any], right: list[Any]) -> list[Any]:
if not left:
return right
@@ -37,7 +40,7 @@ def merge(left, right):
return [right[0], *merge(left, right[1:])]
def tim_sort(lst):
def tim_sort(lst: list[Any] | tuple[Any, ...] | str) -> list[Any]:
"""
>>> tim_sort("Python")
['P', 'h', 'n', 'o', 't', 'y']
@@ -53,7 +56,7 @@ def tim_sort(lst):
length = len(lst)
runs, sorted_runs = [], []
new_run = [lst[0]]
sorted_array = []
sorted_array: list[Any] = []
i = 1
while i < length:
if lst[i] < lst[i - 1]: