Skip to content

Commit 0c0bad8

Browse files
authored
[unittest] Improve annotations of three_way_cmp (#15003)
1 parent 191fa40 commit 0c0bad8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

stdlib/unittest/util.pyi

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
from collections.abc import MutableSequence, Sequence
2-
from typing import Any, Final, TypeVar
2+
from typing import Any, Final, Literal, Protocol, TypeVar, type_check_only
33
from typing_extensions import TypeAlias
44

5+
@type_check_only
6+
class _SupportsDunderLT(Protocol):
7+
def __lt__(self, other: Any, /) -> bool: ...
8+
9+
@type_check_only
10+
class _SupportsDunderGT(Protocol):
11+
def __gt__(self, other: Any, /) -> bool: ...
12+
13+
@type_check_only
14+
class _SupportsDunderLE(Protocol):
15+
def __le__(self, other: Any, /) -> bool: ...
16+
17+
@type_check_only
18+
class _SupportsDunderGE(Protocol):
19+
def __ge__(self, other: Any, /) -> bool: ...
20+
521
_T = TypeVar("_T")
622
_Mismatch: TypeAlias = tuple[_T, _T, int]
23+
_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
724

825
_MAX_LENGTH: Final = 80
926
_PLACEHOLDER_LEN: Final = 12
@@ -18,6 +35,6 @@ def safe_repr(obj: object, short: bool = False) -> str: ...
1835
def strclass(cls: type) -> str: ...
1936
def sorted_list_difference(expected: Sequence[_T], actual: Sequence[_T]) -> tuple[list[_T], list[_T]]: ...
2037
def unorderable_list_difference(expected: MutableSequence[_T], actual: MutableSequence[_T]) -> tuple[list[_T], list[_T]]: ...
21-
def three_way_cmp(x: Any, y: Any) -> int: ...
38+
def three_way_cmp(x: _SupportsComparison, y: _SupportsComparison) -> Literal[-1, 0, 1]: ...
2239
def _count_diff_all_purpose(actual: Sequence[_T], expected: Sequence[_T]) -> list[_Mismatch[_T]]: ...
2340
def _count_diff_hashable(actual: Sequence[_T], expected: Sequence[_T]) -> list[_Mismatch[_T]]: ...

0 commit comments

Comments
 (0)