Skip to content

Commit d4eba8b

Browse files
authored
builtins.sum: Add overload for bool special case (#7975)
Closes #7974
1 parent 8e8176b commit d4eba8b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

stdlib/builtins.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,18 @@ def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparis
15481548
_SumT = TypeVar("_SumT", bound=SupportsAdd)
15491549
_SumS = TypeVar("_SumS", bound=SupportsAdd)
15501550

1551+
# In general, the return type of `x + x` is *not* guaranteed to be the same type as x.
1552+
# However, we can't express that in the stub for `sum()`
1553+
# without creating many false-positive errors (see #7578).
1554+
# Instead, we special-case the most common example of this: bool.
1555+
if sys.version_info >= (3, 8):
1556+
@overload
1557+
def sum(__iterable: Iterable[bool], start: int = ...) -> int: ... # type: ignore[misc]
1558+
1559+
else:
1560+
@overload
1561+
def sum(__iterable: Iterable[bool], __start: int = ...) -> int: ... # type: ignore[misc]
1562+
15511563
@overload
15521564
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...
15531565

0 commit comments

Comments
 (0)