diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index cb1c1bcfc4aa..ea09c97328e9 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -7,6 +7,9 @@ from typing_extensions import ParamSpec, TypeAlias, TypeIs _R = TypeVar("_R") _T = TypeVar("_T") +_T_contra = TypeVar("_T_contra", contravariant=True) +_U = TypeVar("_U") +_U_co = TypeVar("_U_co", covariant=True) _T_co = TypeVar("_T_co", covariant=True) _K = TypeVar("_K") _V = TypeVar("_V") @@ -58,15 +61,45 @@ def truth(a: object, /) -> bool: ... def is_(a: object, b: object, /) -> bool: ... def is_not(a: object, b: object, /) -> bool: ... def abs(a: SupportsAbs[_T], /) -> _T: ... -def add(a: Any, b: Any, /) -> Any: ... -def and_(a: Any, b: Any, /) -> Any: ... + +class _SupportsAdd(Protocol[_T_contra, _U_co]): + def __add__(self, other: _T_contra, /) -> _U_co: ... + +class _SupportsRAdd(Protocol[_T_contra, _U_co]): + def __radd__(self, other: _T_contra, /) -> _U_co: ... + +@overload +def add(a: _SupportsAdd[_T, _U], b: _T, /) -> _U: ... +@overload +def add(a: _T, b: _SupportsRAdd[_T, _U], /) -> _U: ... + +class _SupportsAnd(Protocol[_T_contra, _U_co]): + def __and__(self, other: _T_contra, /) -> _U_co: ... + +class _SupportsRAnd(Protocol[_T_contra, _U_co]): + def __rand__(self, other: _T_contra, /) -> _U_co: ... + +@overload +def and_(a: _SupportsAnd[_T, _U], b: _T, /) -> _U: ... +@overload +def and_(a: _T, b: _SupportsRAnd[_T, _U], /) -> _U: ... def floordiv(a: Any, b: Any, /) -> Any: ... def index(a: SupportsIndex, /) -> int: ... def inv(a: _SupportsInversion[_T_co], /) -> _T_co: ... def invert(a: _SupportsInversion[_T_co], /) -> _T_co: ... def lshift(a: Any, b: Any, /) -> Any: ... def mod(a: Any, b: Any, /) -> Any: ... -def mul(a: Any, b: Any, /) -> Any: ... + +class _SupportsMul(Protocol[_T_contra, _U_co]): + def __mul__(self, other: _T_contra, /) -> _U_co: ... + +class _SupportsRMul(Protocol[_T_contra, _U_co]): + def __rmul__(self, other: _T_contra, /) -> _U_co: ... + +@overload +def mul(a: _SupportsMul[_T, _U], b: _T, /) -> _U: ... +@overload +def mul(a: _T, b: _SupportsRMul[_T, _U], /) -> _U: ... def matmul(a: Any, b: Any, /) -> Any: ... def neg(a: _SupportsNeg[_T_co], /) -> _T_co: ... def or_(a: Any, b: Any, /) -> Any: ...