Skip to content

Commit

Permalink
Move a few protocol from builtins to _typeshed (#7736)
Browse files Browse the repository at this point in the history
  • Loading branch information
srittau authored Apr 28, 2022
1 parent 5535b7f commit 5df8de7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
15 changes: 15 additions & 0 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,27 @@ class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderL
SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001

# Dunder protocols

class SupportsAdd(Protocol):
def __add__(self, __x: Any) -> Any: ...

class SupportsDivMod(Protocol[_T_contra, _T_co]):
def __divmod__(self, __other: _T_contra) -> _T_co: ...

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

# This protocol is generic over the iterator type, while Iterable is
# generic over the type that is iterated over.
class SupportsIter(Protocol[_T_co]):
def __iter__(self) -> _T_co: ...

# This protocol is generic over the iterator type, while AsyncIterable is
# generic over the type that is iterated over.
class SupportsAiter(Protocol[_T_co]):
def __aiter__(self) -> _T_co: ...

class SupportsLenAndGetItem(Protocol[_T_co]):
def __len__(self) -> int: ...
def __getitem__(self, __k: int) -> _T_co: ...
Expand Down
20 changes: 7 additions & 13 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ from _typeshed import (
ReadableBuffer,
Self,
StrOrBytesPath,
SupportsAdd,
SupportsAiter,
SupportsAnext,
SupportsDivMod,
SupportsIter,
SupportsKeysAndGetItem,
SupportsLenAndGetItem,
SupportsNext,
Expand Down Expand Up @@ -71,12 +74,6 @@ _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)

class _SupportsIter(Protocol[_T_co]):
def __iter__(self) -> _T_co: ...

class _SupportsAiter(Protocol[_T_co]):
def __aiter__(self) -> _T_co: ...

class object:
__doc__: str | None
__dict__: dict[str, Any]
Expand Down Expand Up @@ -1094,7 +1091,7 @@ class _PathLike(Protocol[_AnyStr_co]):
def __fspath__(self) -> _AnyStr_co: ...

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

class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
def __anext__(self) -> _AwaitableT_co: ...
Expand Down Expand Up @@ -1186,7 +1183,7 @@ def hex(__number: int | SupportsIndex) -> str: ...
def id(__obj: object) -> int: ...
def input(__prompt: object = ...) -> str: ...
@overload
def iter(__iterable: _SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
@overload
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
@overload
Expand Down Expand Up @@ -1503,11 +1500,8 @@ def sorted(
@overload
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...

class _SupportsSum(Protocol):
def __add__(self, __x: Any) -> Any: ...

_SumT = TypeVar("_SumT", bound=_SupportsSum)
_SumS = TypeVar("_SumS", bound=_SupportsSum)
_SumT = TypeVar("_SumT", bound=SupportsAdd)
_SumS = TypeVar("_SumS", bound=SupportsAdd)

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

0 comments on commit 5df8de7

Please sign in to comment.