Skip to content

Commit

Permalink
Improve stubs for the bisect module
Browse files Browse the repository at this point in the history
The stubs were requiring SupportsRichComparisonT, but bisect only
uses __lt__. Be more specific as to which comparison is actually
required, which depends on the function. This allows using bisect with
types that only implement __lt__.

Closes python#13347.
  • Loading branch information
lmoureaux committed Dec 31, 2024
1 parent 2047b82 commit d2d7a80
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions stdlib/_bisect.pyi
Original file line number Diff line number Diff line change
@@ -1,84 +1,121 @@
import sys
from _typeshed import SupportsLenAndGetItem, SupportsRichComparisonT
from _typeshed import SupportsDunderLT, SupportsLenAndGetItem
from collections.abc import Callable, MutableSequence
from typing import TypeVar, overload

_T = TypeVar("_T")
_U = TypeVar("_U")

if sys.version_info >= (3, 10):
@overload
def bisect_left(
a: SupportsLenAndGetItem[SupportsRichComparisonT],
x: SupportsRichComparisonT,
# Valid comparison: a[i] < x
a: SupportsLenAndGetItem[SupportsDunderLT[_T]],
x: _T,
lo: int = 0,
hi: int | None = None,
*,
key: None = None,
) -> int: ...
@overload
def bisect_left(
# Valid comparison: key(a[i]) < x
a: SupportsLenAndGetItem[_T],
x: SupportsRichComparisonT,
x: _U,
lo: int = 0,
hi: int | None = None,
*,
key: Callable[[_T], SupportsRichComparisonT],
key: Callable[[_T], SupportsDunderLT[_U]],
) -> int: ...
@overload
def bisect_right(
a: SupportsLenAndGetItem[SupportsRichComparisonT],
x: SupportsRichComparisonT,
# Valid comparison: x < a[i]
a: SupportsLenAndGetItem[_T],
x: SupportsDunderLT[_T],
lo: int = 0,
hi: int | None = None,
*,
key: None = None,
) -> int: ...
@overload
def bisect_right(
# Valid comparison: x < key(a[i])
a: SupportsLenAndGetItem[_T],
x: SupportsRichComparisonT,
x: SupportsDunderLT[_U],
lo: int = 0,
hi: int | None = None,
*,
key: Callable[[_T], SupportsRichComparisonT],
key: Callable[[_T], _U],
) -> int: ...
@overload
def insort_left(
a: MutableSequence[SupportsRichComparisonT],
x: SupportsRichComparisonT,
# Valid comparison: a[i] < x
a: MutableSequence[SupportsDunderLT[_T]],
x: _T,
lo: int = 0,
hi: int | None = None,
*,
key: None = None,
) -> None: ...
@overload
def insort_left(
a: MutableSequence[_T], x: _T, lo: int = 0, hi: int | None = None, *, key: Callable[[_T], SupportsRichComparisonT]
# Valid comparison: key(a[i]) < x
a: MutableSequence[_T],
x: _T,
lo: int = 0,
hi: int | None = None,
*,
key: Callable[[_T], SupportsDunderLT[_T]],
) -> None: ...
@overload
def insort_right(
a: MutableSequence[SupportsRichComparisonT],
x: SupportsRichComparisonT,
# Valid comparison: x < a[i]
a: MutableSequence[_T],
x: SupportsDunderLT[_T],
lo: int = 0,
hi: int | None = None,
*,
key: None = None,
) -> None: ...
@overload
def insort_right(
a: MutableSequence[_T], x: _T, lo: int = 0, hi: int | None = None, *, key: Callable[[_T], SupportsRichComparisonT]
# Valid comparison: x < key(a[i])
a: MutableSequence[_T],
x: SupportsDunderLT[_U],
lo: int = 0,
hi: int | None = None,
*,
key: Callable[[_T], _U],
) -> None: ...

else:
def bisect_left(
a: SupportsLenAndGetItem[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
# Valid comparison: a[i] < x
a: SupportsLenAndGetItem[SupportsDunderLT[_T]],
x: _T,
lo: int = 0,
hi: int | None = None,
) -> int: ...
def bisect_right(
a: SupportsLenAndGetItem[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
# Valid comparison: x < a[i]
a: SupportsLenAndGetItem[_T],
x: SupportsDunderLT[_T],
lo: int = 0,
hi: int | None = None,
) -> int: ...
def insort_left(
a: MutableSequence[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
# Valid comparison: a[i] < x
a: MutableSequence[SupportsDunderLT[_T]],
x: _T,
lo: int = 0,
hi: int | None = None,
) -> None: ...
def insort_right(
a: MutableSequence[SupportsRichComparisonT], x: SupportsRichComparisonT, lo: int = 0, hi: int | None = None
# Valid comparison: x < a[i]
a: MutableSequence[_T],
x: SupportsDunderLT[_T],
lo: int = 0,
hi: int | None = None,
*,
key: None = None,
) -> None: ...

0 comments on commit d2d7a80

Please sign in to comment.