From 2eb0ffab1ba32d7f4112144c5eab2d3156a17ca4 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 30 Nov 2021 21:08:17 +0000 Subject: [PATCH 1/2] Improve BinOp comparison funcs in `operator` --- stdlib/_operator.pyi | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index b337d84da4cb..b491abf7ce79 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -14,6 +14,7 @@ from typing import ( SupportsAbs, Tuple, TypeVar, + Union, overload, ) from typing_extensions import ParamSpec, SupportsIndex, final @@ -34,12 +35,29 @@ class _SupportsNeg(Protocol[_T_co]): class _SupportsPos(Protocol[_T_co]): def __pos__(self) -> _T_co: ... -def lt(__a: Any, __b: Any) -> Any: ... -def le(__a: Any, __b: Any) -> Any: ... +# Different to _typeshed.SupportsLessThan +class _SupportsLT(Protocol): + def __lt__(self, other: Any) -> Any: ... + +class _SupportsGT(Protocol): + def __gt__(self, other: Any) -> Any: ... + +_SupportsGTorLT = Union[_SupportsGT, _SupportsLT] + +class _SupportsLE(Protocol): + def __le__(self, other: Any) -> Any: ... + +class _SupportsGE(Protocol): + def __ge__(self, other: Any) -> Any: ... + +_SupportsGEorLE = Union[_SupportsLE, _SupportsGE] + +def lt(__a: _SupportsGTorLT, __b: _SupportsGTorLT) -> Any: ... +def le(__a: _SupportsGEorLE, __b: _SupportsGEorLE) -> Any: ... def eq(__a: object, __b: object) -> Any: ... def ne(__a: object, __b: object) -> Any: ... -def ge(__a: Any, __b: Any) -> Any: ... -def gt(__a: Any, __b: Any) -> Any: ... +def ge(__a: _SupportsGEorLE, __b: _SupportsGEorLE) -> Any: ... +def gt(__a: _SupportsGTorLT, __b: _SupportsGTorLT) -> Any: ... def not_(__a: object) -> bool: ... def truth(__a: object) -> bool: ... def is_(__a: object, __b: object) -> bool: ... From 5f4ea84168b557ab38729a7a842e013250b7c893 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 30 Nov 2021 23:26:12 +0000 Subject: [PATCH 2/2] Make less precise --- stdlib/_operator.pyi | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index b491abf7ce79..a945a0be74a4 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -42,22 +42,20 @@ class _SupportsLT(Protocol): class _SupportsGT(Protocol): def __gt__(self, other: Any) -> Any: ... -_SupportsGTorLT = Union[_SupportsGT, _SupportsLT] - class _SupportsLE(Protocol): def __le__(self, other: Any) -> Any: ... class _SupportsGE(Protocol): def __ge__(self, other: Any) -> Any: ... -_SupportsGEorLE = Union[_SupportsLE, _SupportsGE] +_SupportsComparison = Union[_SupportsLE, _SupportsGE, _SupportsGT, _SupportsLT] -def lt(__a: _SupportsGTorLT, __b: _SupportsGTorLT) -> Any: ... -def le(__a: _SupportsGEorLE, __b: _SupportsGEorLE) -> Any: ... +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: _SupportsGEorLE, __b: _SupportsGEorLE) -> Any: ... -def gt(__a: _SupportsGTorLT, __b: _SupportsGTorLT) -> Any: ... +def ge(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ... +def gt(__a: _SupportsComparison, __b: _SupportsComparison) -> Any: ... def not_(__a: object) -> bool: ... def truth(__a: object) -> bool: ... def is_(__a: object, __b: object) -> bool: ...