Skip to content

Commit 5df8de7

Browse files
authored
Move a few protocol from builtins to _typeshed (#7736)
1 parent 5535b7f commit 5df8de7

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,27 @@ class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderL
6464
SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT
6565
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001
6666

67+
# Dunder protocols
68+
69+
class SupportsAdd(Protocol):
70+
def __add__(self, __x: Any) -> Any: ...
71+
6772
class SupportsDivMod(Protocol[_T_contra, _T_co]):
6873
def __divmod__(self, __other: _T_contra) -> _T_co: ...
6974

7075
class SupportsRDivMod(Protocol[_T_contra, _T_co]):
7176
def __rdivmod__(self, __other: _T_contra) -> _T_co: ...
7277

78+
# This protocol is generic over the iterator type, while Iterable is
79+
# generic over the type that is iterated over.
80+
class SupportsIter(Protocol[_T_co]):
81+
def __iter__(self) -> _T_co: ...
82+
83+
# This protocol is generic over the iterator type, while AsyncIterable is
84+
# generic over the type that is iterated over.
85+
class SupportsAiter(Protocol[_T_co]):
86+
def __aiter__(self) -> _T_co: ...
87+
7388
class SupportsLenAndGetItem(Protocol[_T_co]):
7489
def __len__(self) -> int: ...
7590
def __getitem__(self, __k: int) -> _T_co: ...

stdlib/builtins.pyi

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ from _typeshed import (
1111
ReadableBuffer,
1212
Self,
1313
StrOrBytesPath,
14+
SupportsAdd,
15+
SupportsAiter,
1416
SupportsAnext,
1517
SupportsDivMod,
18+
SupportsIter,
1619
SupportsKeysAndGetItem,
1720
SupportsLenAndGetItem,
1821
SupportsNext,
@@ -71,12 +74,6 @@ _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant
7174
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
7275
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
7376

74-
class _SupportsIter(Protocol[_T_co]):
75-
def __iter__(self) -> _T_co: ...
76-
77-
class _SupportsAiter(Protocol[_T_co]):
78-
def __aiter__(self) -> _T_co: ...
79-
8077
class object:
8178
__doc__: str | None
8279
__dict__: dict[str, Any]
@@ -1094,7 +1091,7 @@ class _PathLike(Protocol[_AnyStr_co]):
10941091
def __fspath__(self) -> _AnyStr_co: ...
10951092

10961093
if sys.version_info >= (3, 10):
1097-
def aiter(__async_iterable: _SupportsAiter[_SupportsAnextT]) -> _SupportsAnextT: ...
1094+
def aiter(__async_iterable: SupportsAiter[_SupportsAnextT]) -> _SupportsAnextT: ...
10981095

10991096
class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
11001097
def __anext__(self) -> _AwaitableT_co: ...
@@ -1186,7 +1183,7 @@ def hex(__number: int | SupportsIndex) -> str: ...
11861183
def id(__obj: object) -> int: ...
11871184
def input(__prompt: object = ...) -> str: ...
11881185
@overload
1189-
def iter(__iterable: _SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
1186+
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
11901187
@overload
11911188
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
11921189
@overload
@@ -1503,11 +1500,8 @@ def sorted(
15031500
@overload
15041501
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...
15051502

1506-
class _SupportsSum(Protocol):
1507-
def __add__(self, __x: Any) -> Any: ...
1508-
1509-
_SumT = TypeVar("_SumT", bound=_SupportsSum)
1510-
_SumS = TypeVar("_SumS", bound=_SupportsSum)
1503+
_SumT = TypeVar("_SumT", bound=SupportsAdd)
1504+
_SumS = TypeVar("_SumS", bound=SupportsAdd)
15111505

15121506
@overload
15131507
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...

0 commit comments

Comments
 (0)