From f41f186c4ec37b6ad712013eca1d6f7962e1d4cf Mon Sep 17 00:00:00 2001 From: jpy-git Date: Fri, 1 Apr 2022 21:20:08 +0100 Subject: [PATCH 1/3] `sum`: `Protocol` defining `__add__` --- stdlib/builtins.pyi | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 99783cd60afc..9372b2a18ae6 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1477,16 +1477,23 @@ def sorted( ) -> list[SupportsRichComparisonT]: ... @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) + @overload -def sum(__iterable: Iterable[_T]) -> _T | Literal[0]: ... +def sum(__iterable: Iterable[_SumT]) -> Any | Literal[0]: ... if sys.version_info >= (3, 8): @overload - def sum(__iterable: Iterable[_T], start: _S) -> _T | _S: ... + def sum(__iterable: Iterable[_SumT], start: _SumS) -> _SumT | _SumS: ... else: @overload - def sum(__iterable: Iterable[_T], __start: _S) -> _T | _S: ... + def sum(__iterable: Iterable[_SumT], __start: _SumS) -> _SumT | _SumS: ... # The argument to `vars()` has to have a `__dict__` attribute, so can't be annotated with `object` # (A "SupportsDunderDict" protocol doesn't work) From 810250b3aabbd6a858a2738069f52d31529f16ab Mon Sep 17 00:00:00 2001 From: jpy-git Date: Fri, 1 Apr 2022 21:40:11 +0100 Subject: [PATCH 2/3] Make Protocol generic --- stdlib/builtins.pyi | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 9372b2a18ae6..6da154c7627b 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1478,22 +1478,19 @@ 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) +class _SupportsSum(Protocol[_T]): + def __add__(self, __x: _T) -> _T: ... @overload -def sum(__iterable: Iterable[_SumT]) -> Any | Literal[0]: ... +def sum(__iterable: Iterable[_SupportsSum[_T]]) -> _T | Literal[0]: ... if sys.version_info >= (3, 8): @overload - def sum(__iterable: Iterable[_SumT], start: _SumS) -> _SumT | _SumS: ... + def sum(__iterable: Iterable[_SupportsSum[_T]], start: _SupportsSum[_S]) -> _SupportsSum[_T] | _SupportsSum[_S]: ... else: @overload - def sum(__iterable: Iterable[_SumT], __start: _SumS) -> _SumT | _SumS: ... + def sum(__iterable: Iterable[_SupportsSum[_T]], __start: _SupportsSum[_S]) -> _SupportsSum[_T] | _SupportsSum[_S]: ... # The argument to `vars()` has to have a `__dict__` attribute, so can't be annotated with `object` # (A "SupportsDunderDict" protocol doesn't work) From ae316c3331218a719a12633c899119cb9ae86a47 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Fri, 1 Apr 2022 21:51:10 +0100 Subject: [PATCH 3/3] Revert to TypeVars --- stdlib/builtins.pyi | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 6da154c7627b..ce2042bcac63 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1478,19 +1478,22 @@ def sorted( @overload def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ... -class _SupportsSum(Protocol[_T]): - def __add__(self, __x: _T) -> _T: ... +class _SupportsSum(Protocol): + def __add__(self, __x: Any) -> Any: ... + +_SumT = TypeVar("_SumT", bound=_SupportsSum) +_SumS = TypeVar("_SumS", bound=_SupportsSum) @overload -def sum(__iterable: Iterable[_SupportsSum[_T]]) -> _T | Literal[0]: ... +def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ... if sys.version_info >= (3, 8): @overload - def sum(__iterable: Iterable[_SupportsSum[_T]], start: _SupportsSum[_S]) -> _SupportsSum[_T] | _SupportsSum[_S]: ... + def sum(__iterable: Iterable[_SumT], start: _SumS) -> _SumT | _SumS: ... else: @overload - def sum(__iterable: Iterable[_SupportsSum[_T]], __start: _SupportsSum[_S]) -> _SupportsSum[_T] | _SupportsSum[_S]: ... + def sum(__iterable: Iterable[_SumT], __start: _SumS) -> _SumT | _SumS: ... # The argument to `vars()` has to have a `__dict__` attribute, so can't be annotated with `object` # (A "SupportsDunderDict" protocol doesn't work)