builtins.sum: Add overload for bool special case (#7975)

Closes #7974
This commit is contained in:
Alex Waygood
2022-05-28 19:14:10 +01:00
committed by GitHub
parent 8e8176b24c
commit d4eba8b5ce

View File

@@ -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]: ...