Skip to content

Commit 260bd19

Browse files
committed
Partial typeshed cherry-pick: Add SupportsRichComparison type to _typeshed (#6583)
The original PR doesn't apply cleanly so I only included the changes to builtins to avoid having to manually deal with all the conflicts.
1 parent e1210cc commit 260bd19

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ class SupportsGreaterThan(Protocol):
4545

4646
SupportsGreaterThanT = TypeVar("SupportsGreaterThanT", bound=SupportsGreaterThan) # noqa: Y001
4747

48+
# Comparison protocols
49+
50+
class SupportsDunderLT(Protocol):
51+
def __lt__(self, __other: Any) -> Any: ...
52+
53+
class SupportsDunderGT(Protocol):
54+
def __gt__(self, __other: Any) -> Any: ...
55+
56+
class SupportsDunderLE(Protocol):
57+
def __le__(self, __other: Any) -> Any: ...
58+
59+
class SupportsDunderGE(Protocol):
60+
def __ge__(self, __other: Any) -> Any: ...
61+
62+
class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderLE, SupportsDunderGE, Protocol): ...
63+
64+
SupportsRichComparison = Union[SupportsDunderLT, SupportsDunderGT]
65+
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001
66+
SupportsAnyComparison = Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]
67+
4868
class SupportsDivMod(Protocol[_T_contra, _T_co]):
4969
def __divmod__(self, __other: _T_contra) -> _T_co: ...
5070

mypy/typeshed/stdlib/builtins.pyi

+20-22
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ from _typeshed import (
1313
StrOrBytesPath,
1414
SupportsAnext,
1515
SupportsDivMod,
16-
SupportsGreaterThan,
17-
SupportsGreaterThanT,
1816
SupportsKeysAndGetItem,
1917
SupportsLenAndGetItem,
20-
SupportsLessThan,
21-
SupportsLessThanT,
2218
SupportsNext,
2319
SupportsRDivMod,
20+
SupportsRichComparison,
21+
SupportsRichComparisonT,
2422
SupportsTrunc,
2523
SupportsWrite,
2624
)
@@ -779,9 +777,9 @@ class list(MutableSequence[_T], Generic[_T]):
779777
def remove(self, __value: _T) -> None: ...
780778
def reverse(self) -> None: ...
781779
@overload
782-
def sort(self: list[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ...
780+
def sort(self: list[SupportsRichComparisonT], *, key: None = ..., reverse: bool = ...) -> None: ...
783781
@overload
784-
def sort(self, *, key: Callable[[_T], SupportsLessThan], reverse: bool = ...) -> None: ...
782+
def sort(self, *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> None: ...
785783
def __len__(self) -> int: ...
786784
def __iter__(self) -> Iterator[_T]: ...
787785
def __str__(self) -> str: ...
@@ -1146,32 +1144,32 @@ class map(Iterator[_S], Generic[_S]):
11461144

11471145
@overload
11481146
def max(
1149-
__arg1: SupportsGreaterThanT, __arg2: SupportsGreaterThanT, *_args: SupportsGreaterThanT, key: None = ...
1150-
) -> SupportsGreaterThanT: ...
1147+
__arg1: SupportsRichComparisonT, __arg2: SupportsRichComparisonT, *_args: SupportsRichComparisonT, key: None = ...
1148+
) -> SupportsRichComparisonT: ...
11511149
@overload
1152-
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsGreaterThan]) -> _T: ...
1150+
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11531151
@overload
1154-
def max(__iterable: Iterable[SupportsGreaterThanT], *, key: None = ...) -> SupportsGreaterThanT: ...
1152+
def max(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ...) -> SupportsRichComparisonT: ...
11551153
@overload
1156-
def max(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsGreaterThan]) -> _T: ...
1154+
def max(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11571155
@overload
1158-
def max(__iterable: Iterable[SupportsGreaterThanT], *, key: None = ..., default: _T) -> SupportsGreaterThanT | _T: ...
1156+
def max(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., default: _T) -> SupportsRichComparisonT | _T: ...
11591157
@overload
1160-
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsGreaterThan], default: _T2) -> _T1 | _T2: ...
1158+
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsRichComparison], default: _T2) -> _T1 | _T2: ...
11611159
@overload
11621160
def min(
1163-
__arg1: SupportsLessThanT, __arg2: SupportsLessThanT, *_args: SupportsLessThanT, key: None = ...
1164-
) -> SupportsLessThanT: ...
1161+
__arg1: SupportsRichComparisonT, __arg2: SupportsRichComparisonT, *_args: SupportsRichComparisonT, key: None = ...
1162+
) -> SupportsRichComparisonT: ...
11651163
@overload
1166-
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsLessThan]) -> _T: ...
1164+
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11671165
@overload
1168-
def min(__iterable: Iterable[SupportsLessThanT], *, key: None = ...) -> SupportsLessThanT: ...
1166+
def min(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ...) -> SupportsRichComparisonT: ...
11691167
@overload
1170-
def min(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsLessThan]) -> _T: ...
1168+
def min(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
11711169
@overload
1172-
def min(__iterable: Iterable[SupportsLessThanT], *, key: None = ..., default: _T) -> SupportsLessThanT | _T: ...
1170+
def min(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., default: _T) -> SupportsRichComparisonT | _T: ...
11731171
@overload
1174-
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsLessThan], default: _T2) -> _T1 | _T2: ...
1172+
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsRichComparison], default: _T2) -> _T1 | _T2: ...
11751173
@overload
11761174
def next(__i: SupportsNext[_T]) -> _T: ...
11771175
@overload
@@ -1382,9 +1380,9 @@ def round(number: SupportsRound[_T], ndigits: SupportsIndex) -> _T: ...
13821380
# for why arg 3 of `setattr` should be annotated with `Any` and not `object`
13831381
def setattr(__obj: object, __name: str, __value: Any) -> None: ...
13841382
@overload
1385-
def sorted(__iterable: Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> list[SupportsLessThanT]: ...
1383+
def sorted(__iterable: Iterable[SupportsRichComparisonT], *, key: None = ..., reverse: bool = ...) -> list[SupportsRichComparisonT]: ...
13861384
@overload
1387-
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsLessThan], reverse: bool = ...) -> list[_T]: ...
1385+
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...
13881386

13891387
if sys.version_info >= (3, 8):
13901388
@overload

0 commit comments

Comments
 (0)