From d3053ae80d75faaf6fedfaee8788fa03727b94af Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Sat, 14 Nov 2026 20:54:41 -0800 Subject: [PATCH 1/4] Allow arbitrary rich comparison results in operator --- stdlib/@tests/test_cases/check_operator.py | 24 ++++++++++++++++++++++ stdlib/_operator.pyi | 23 ++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 stdlib/@tests/test_cases/check_operator.py diff --git a/stdlib/@tests/test_cases/check_operator.py b/stdlib/@tests/test_cases/check_operator.py new file mode 100644 index 000000000000..5277f9d47f96 --- /dev/null +++ b/stdlib/@tests/test_cases/check_operator.py @@ -0,0 +1,24 @@ +import operator + + +class Result: ... + + +class Operand: ... + + +class LessThan(Operand): + def __lt__(self, other: Operand, /) -> Result: + return Result() + + +class GreaterEqual: + def __ge__(self, other: Operand, /) -> Result: + return Result() + + +def check_comparisons(lt: LessThan, ge: GreaterEqual) -> None: + operator.lt(lt, lt) + operator.gt(lt, lt) + operator.ge(ge, lt) + operator.le(lt, ge) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index ac7d64f2ee08..f420b9d63a7e 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -2,10 +2,6 @@ import sys from _typeshed import ( ReadableBuffer, SupportsAdd, - SupportsDunderGE, - SupportsDunderGT, - SupportsDunderLE, - SupportsDunderLT, SupportsGetItem, SupportsMul, SupportsRAdd, @@ -26,7 +22,24 @@ _K = TypeVar("_K") _V = TypeVar("_V") _P = ParamSpec("_P") -_SupportsComparison: TypeAlias = SupportsDunderLE[Any] | SupportsDunderGE[Any] | SupportsDunderGT[Any] | SupportsDunderLT[Any] +# operator returns the rich comparison result without testing its truth value. +@type_check_only +class _SupportsDunderLT(Protocol): + def __lt__(self, other: Any, /) -> object: ... + +@type_check_only +class _SupportsDunderGT(Protocol): + def __gt__(self, other: Any, /) -> object: ... + +@type_check_only +class _SupportsDunderLE(Protocol): + def __le__(self, other: Any, /) -> object: ... + +@type_check_only +class _SupportsDunderGE(Protocol): + def __ge__(self, other: Any, /) -> object: ... + +_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT @type_check_only class _SupportsInversion(Protocol[_T_co]): From 1aec53541c82042dfacf0e816691bac5be120765 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Sat, 14 Nov 2026 20:55:06 -0800 Subject: [PATCH 2/4] Accept ordering comparisons implemented by either operand --- stdlib/@tests/test_cases/check_operator.py | 25 +++++++++ stdlib/_operator.pyi | 59 ++++++++++++++++------ 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/stdlib/@tests/test_cases/check_operator.py b/stdlib/@tests/test_cases/check_operator.py index 5277f9d47f96..2861908ad25c 100644 --- a/stdlib/@tests/test_cases/check_operator.py +++ b/stdlib/@tests/test_cases/check_operator.py @@ -1,4 +1,5 @@ import operator +from typing import overload class Result: ... @@ -22,3 +23,27 @@ def check_comparisons(lt: LessThan, ge: GreaterEqual) -> None: operator.gt(lt, lt) operator.ge(ge, lt) operator.le(lt, ge) + + +def check_one_sided_comparisons(lt: LessThan, ge: GreaterEqual, other: Operand) -> None: + operator.lt(lt, other) + operator.gt(other, lt) + operator.ge(ge, other) + operator.le(other, ge) + operator.lt(1, other) # type: ignore[misc] + + operators = {"<": operator.lt, "<=": operator.le, ">": operator.gt, ">=": operator.ge, "==": operator.eq, "!=": operator.ne} + operators["<"](lt, other) + + +class Overloaded: + @overload + def __lt__(self, other: int, /) -> int: ... + @overload + def __lt__(self, other: str, /) -> str: ... + def __lt__(self, other: int | str, /) -> int | str: + return other + + +def check_overloaded(value: Overloaded, other: int | str) -> None: + operator.lt(value, other) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index f420b9d63a7e..083c18d5c708 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -24,22 +24,29 @@ _P = ParamSpec("_P") # operator returns the rich comparison result without testing its truth value. @type_check_only -class _SupportsDunderLT(Protocol): - def __lt__(self, other: Any, /) -> object: ... +class _SupportsDunderLT(Protocol[_T_contra]): + def __lt__(self, other: _T_contra, /) -> object: ... @type_check_only -class _SupportsDunderGT(Protocol): - def __gt__(self, other: Any, /) -> object: ... +class _SupportsDunderGT(Protocol[_T_contra]): + def __gt__(self, other: _T_contra, /) -> object: ... @type_check_only -class _SupportsDunderLE(Protocol): - def __le__(self, other: Any, /) -> object: ... +class _SupportsDunderLE(Protocol[_T_contra]): + def __le__(self, other: _T_contra, /) -> object: ... @type_check_only -class _SupportsDunderGE(Protocol): - def __ge__(self, other: Any, /) -> object: ... +class _SupportsDunderGE(Protocol[_T_contra]): + def __ge__(self, other: _T_contra, /) -> object: ... -_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT +_SupportsComparison: TypeAlias = _SupportsDunderLT[_T] | _SupportsDunderGT[_T] | _SupportsDunderLE[_T] | _SupportsDunderGE[_T] + +# _T is the argument type accepted by the other operand's ordering method. +# _SupportsComparison[Any] preserves the rule that any two operands with +# ordering methods are accepted. For example, if x.__lt__ has separate int and +# str overloads and y is int | str, neither overload accepts the whole union. +# The fallback accepts lt(x, y) because int and str each define an ordering method. +_ComparisonOperand: TypeAlias = _T | _SupportsComparison[Any] @type_check_only class _SupportsInversion(Protocol[_T_co]): @@ -190,13 +197,33 @@ class _SupportsIConcat(Protocol[_T_contra, _T_co]): def __iadd__(self, value: _T_contra, /) -> _T_co: ... def __getitem__(self, key: Any) -> Any: ... # method needs only exist -# All four comparison functions must have the same signature, or we get false-positive errors -def lt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ... -def le(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ... -def eq(a: object, b: object, /) -> Any: ... -def ne(a: object, b: object, /) -> Any: ... -def ge(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ... -def gt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ... +# Either operand can implement the comparison. Dispatch tables rely on all four +# ordering functions having the same signatures (#6462). +@overload +def lt(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +@overload +def lt(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... + +@overload +def le(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +@overload +def le(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... + +# Equality accepts any operands. Using Any also lets mypy infer a callable +# for a dispatch table containing both equality and ordering functions. +def eq(a: Any, b: Any, /) -> Any: ... +def ne(a: Any, b: Any, /) -> Any: ... + +@overload +def ge(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +@overload +def ge(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... + +@overload +def gt(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +@overload +def gt(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... + def not_(a: object, /) -> bool: ... def truth(a: object, /) -> bool: ... def is_(a: object, b: object, /) -> bool: ... From 40f6333696d928d458fdf9a87f9cf06f9bd6b64c Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Sat, 14 Nov 2026 21:42:24 -0800 Subject: [PATCH 3/4] Preserve the unsubscripted comparison alias used by pywin32 --- stdlib/_operator.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index 083c18d5c708..19c69ca58cd9 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -39,7 +39,11 @@ class _SupportsDunderLE(Protocol[_T_contra]): class _SupportsDunderGE(Protocol[_T_contra]): def __ge__(self, other: _T_contra, /) -> object: ... -_SupportsComparison: TypeAlias = _SupportsDunderLT[_T] | _SupportsDunderGT[_T] | _SupportsDunderLE[_T] | _SupportsDunderGE[_T] +# Some projects use _SupportsComparison without a type argument. +_OperandT = TypeVar("_OperandT", default=Any) +_SupportsComparison: TypeAlias = ( + _SupportsDunderLT[_OperandT] | _SupportsDunderGT[_OperandT] | _SupportsDunderLE[_OperandT] | _SupportsDunderGE[_OperandT] +) # _T is the argument type accepted by the other operand's ordering method. # _SupportsComparison[Any] preserves the rule that any two operands with From 573a07c0d5e6dd044db38b48bb1723946674caae Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Sun, 15 Nov 2026 08:32:33 -0800 Subject: [PATCH 4/4] Keep _SupportsComparison non-generic and clarify ordering overloads --- stdlib/_operator.pyi | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index 19c69ca58cd9..ca0a7a985856 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -22,7 +22,8 @@ _K = TypeVar("_K") _V = TypeVar("_V") _P = ParamSpec("_P") -# operator returns the rich comparison result without testing its truth value. +# operator returns the rich comparison result without testing its truth value, so these protocols +# return `object` (unlike the similar protocols in `_typeshed`). @type_check_only class _SupportsDunderLT(Protocol[_T_contra]): def __lt__(self, other: _T_contra, /) -> object: ... @@ -39,18 +40,15 @@ class _SupportsDunderLE(Protocol[_T_contra]): class _SupportsDunderGE(Protocol[_T_contra]): def __ge__(self, other: _T_contra, /) -> object: ... -# Some projects use _SupportsComparison without a type argument. -_OperandT = TypeVar("_OperandT", default=Any) -_SupportsComparison: TypeAlias = ( - _SupportsDunderLT[_OperandT] | _SupportsDunderGT[_OperandT] | _SupportsDunderLE[_OperandT] | _SupportsDunderGE[_OperandT] -) +_SupportsComparisonWith: TypeAlias = _SupportsDunderLT[_T] | _SupportsDunderGT[_T] | _SupportsDunderLE[_T] | _SupportsDunderGE[_T] +_SupportsComparison: TypeAlias = _SupportsComparisonWith[Any] # _T is the argument type accepted by the other operand's ordering method. -# _SupportsComparison[Any] preserves the rule that any two operands with +# _SupportsComparison preserves the rule that any two operands with # ordering methods are accepted. For example, if x.__lt__ has separate int and # str overloads and y is int | str, neither overload accepts the whole union. # The fallback accepts lt(x, y) because int and str each define an ordering method. -_ComparisonOperand: TypeAlias = _T | _SupportsComparison[Any] +_ComparisonOperand: TypeAlias = _T | _SupportsComparison @type_check_only class _SupportsInversion(Protocol[_T_co]): @@ -201,32 +199,32 @@ class _SupportsIConcat(Protocol[_T_contra, _T_co]): def __iadd__(self, value: _T_contra, /) -> _T_co: ... def __getitem__(self, key: Any) -> Any: ... # method needs only exist -# Either operand can implement the comparison. Dispatch tables rely on all four -# ordering functions having the same signatures (#6462). +# Either operand can implement the comparison. Keep the four ordering functions' signatures +# identical so mypy infers a common callable type when they are stored in a dictionary (see check_operator.py). @overload -def lt(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +def lt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ... @overload -def lt(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... +def lt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ... @overload -def le(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +def le(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ... @overload -def le(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... +def le(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ... -# Equality accepts any operands. Using Any also lets mypy infer a callable -# for a dispatch table containing both equality and ordering functions. +# Equality accepts any operands. Using Any lets mypy infer a callable value type for +# ops = {"<": lt, "==": eq}, so ops[symbol](a, b) type-checks. def eq(a: Any, b: Any, /) -> Any: ... def ne(a: Any, b: Any, /) -> Any: ... @overload -def ge(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +def ge(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ... @overload -def ge(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... +def ge(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ... @overload -def gt(a: _SupportsComparison[_T], b: _ComparisonOperand[_T], /) -> Any: ... +def gt(a: _SupportsComparisonWith[_T], b: _ComparisonOperand[_T], /) -> Any: ... @overload -def gt(a: _ComparisonOperand[_T], b: _SupportsComparison[_T], /) -> Any: ... +def gt(a: _ComparisonOperand[_T], b: _SupportsComparisonWith[_T], /) -> Any: ... def not_(a: object, /) -> bool: ... def truth(a: object, /) -> bool: ...