Skip to content

Commit 772b45e

Browse files
authored
[_operator] Improve ordering comparisons (#16812)
1 parent 6a467d7 commit 772b45e

2 files changed

Lines changed: 104 additions & 12 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import operator
2+
from typing import overload
3+
4+
5+
class Result: ...
6+
7+
8+
class Operand: ...
9+
10+
11+
class LessThan(Operand):
12+
def __lt__(self, other: Operand, /) -> Result:
13+
return Result()
14+
15+
16+
class GreaterEqual:
17+
def __ge__(self, other: Operand, /) -> Result:
18+
return Result()
19+
20+
21+
def check_comparisons(lt: LessThan, ge: GreaterEqual) -> None:
22+
operator.lt(lt, lt)
23+
operator.gt(lt, lt)
24+
operator.ge(ge, lt)
25+
operator.le(lt, ge)
26+
27+
28+
def check_one_sided_comparisons(lt: LessThan, ge: GreaterEqual, other: Operand) -> None:
29+
operator.lt(lt, other)
30+
operator.gt(other, lt)
31+
operator.ge(ge, other)
32+
operator.le(other, ge)
33+
operator.lt(1, other) # type: ignore[misc]
34+
35+
operators = {"<": operator.lt, "<=": operator.le, ">": operator.gt, ">=": operator.ge, "==": operator.eq, "!=": operator.ne}
36+
operators["<"](lt, other)
37+
38+
39+
class Overloaded:
40+
@overload
41+
def __lt__(self, other: int, /) -> int: ...
42+
@overload
43+
def __lt__(self, other: str, /) -> str: ...
44+
def __lt__(self, other: int | str, /) -> int | str:
45+
return other
46+
47+
48+
def check_overloaded(value: Overloaded, other: int | str) -> None:
49+
operator.lt(value, other)

‎stdlib/_operator.pyi‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import sys
22
from _typeshed import (
33
ReadableBuffer,
44
SupportsAdd,
5-
SupportsDunderGE,
6-
SupportsDunderGT,
7-
SupportsDunderLE,
8-
SupportsDunderLT,
95
SupportsGetItem,
106
SupportsMul,
117
SupportsRAdd,
@@ -26,7 +22,34 @@ _K = TypeVar("_K")
2622
_V = TypeVar("_V")
2723
_P = ParamSpec("_P")
2824

29-
_SupportsComparison: TypeAlias = SupportsDunderLE[Any] | SupportsDunderGE[Any] | SupportsDunderGT[Any] | SupportsDunderLT[Any]
25+
# The ordering functions in `operator` return the comparison result without testing its truth value.
26+
# The ordering protocols in `_typeshed` require `SupportsBool`, so we use local protocols
27+
# whose methods can return any object.
28+
@type_check_only
29+
class _SupportsDunderLT(Protocol[_T_contra]):
30+
def __lt__(self, other: _T_contra, /) -> object: ...
31+
32+
@type_check_only
33+
class _SupportsDunderGT(Protocol[_T_contra]):
34+
def __gt__(self, other: _T_contra, /) -> object: ...
35+
36+
@type_check_only
37+
class _SupportsDunderLE(Protocol[_T_contra]):
38+
def __le__(self, other: _T_contra, /) -> object: ...
39+
40+
@type_check_only
41+
class _SupportsDunderGE(Protocol[_T_contra]):
42+
def __ge__(self, other: _T_contra, /) -> object: ...
43+
44+
_SupportsComparisonWith: TypeAlias = _SupportsDunderLT[_T] | _SupportsDunderGT[_T] | _SupportsDunderLE[_T] | _SupportsDunderGE[_T]
45+
_SupportsComparison: TypeAlias = _SupportsComparisonWith[Any]
46+
47+
# _T is the argument type accepted by the other operand's ordering method.
48+
# _SupportsComparison preserves the rule that any two operands with
49+
# ordering methods are accepted. For example, if x.__lt__ has separate int and
50+
# str overloads and y is int | str, neither overload accepts the whole union.
51+
# The fallback accepts lt(x, y) because int and str each define an ordering method.
52+
_ComparisonOperand: TypeAlias = _T | _SupportsComparison
3053

3154
@type_check_only
3255
class _SupportsInversion(Protocol[_T_co]):
@@ -177,13 +200,33 @@ class _SupportsIConcat(Protocol[_T_contra, _T_co]):
177200
def __iadd__(self, value: _T_contra, /) -> _T_co: ...
178201
def __getitem__(self, key: Any) -> Any: ... # method needs only exist
179202

180-
# All four comparison functions must have the same signature, or we get false-positive errors
181-
def lt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
182-
def le(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
183-
def eq(a: object, b: object, /) -> Any: ...
184-
def ne(a: object, b: object, /) -> Any: ...
185-
def ge(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
186-
def gt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
203+
# Either operand can implement the comparison. Keep the four ordering functions' signatures
204+
# identical so mypy infers a common callable type when they are stored in a dictionary (see check_operator.py).
205+
@overload
206+
def lt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
207+
@overload
208+
def lt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...
209+
210+
@overload
211+
def le(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
212+
@overload
213+
def le(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...
214+
215+
# Equality accepts any operands. Using Any lets mypy infer a callable value type for
216+
# ops = {"<": lt, "==": eq}, so ops[symbol](a, b) type-checks.
217+
def eq(a: Any, b: Any, /) -> Any: ...
218+
def ne(a: Any, b: Any, /) -> Any: ...
219+
220+
@overload
221+
def ge(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
222+
@overload
223+
def ge(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...
224+
225+
@overload
226+
def gt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ...
227+
@overload
228+
def gt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ...
229+
187230
def not_(a: object, /) -> bool: ...
188231
def truth(a: object, /) -> bool: ...
189232
def is_(a: object, b: object, /) -> bool: ...

0 commit comments

Comments
 (0)