From d4eba8b5ceb38576b1fee1133196d3b99cf2b344 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 28 May 2022 19:14:10 +0100 Subject: [PATCH] `builtins.sum`: Add overload for `bool` special case (#7975) Closes #7974 --- stdlib/builtins.pyi | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 1f49f1317..53b3cdd06 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1548,6 +1548,18 @@ def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparis _SumT = TypeVar("_SumT", bound=SupportsAdd) _SumS = TypeVar("_SumS", bound=SupportsAdd) +# In general, the return type of `x + x` is *not* guaranteed to be the same type as x. +# However, we can't express that in the stub for `sum()` +# without creating many false-positive errors (see #7578). +# Instead, we special-case the most common example of this: bool. +if sys.version_info >= (3, 8): + @overload + def sum(__iterable: Iterable[bool], start: int = ...) -> int: ... # type: ignore[misc] + +else: + @overload + def sum(__iterable: Iterable[bool], __start: int = ...) -> int: ... # type: ignore[misc] + @overload def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...