Skip to content

Commit

Permalink
builtins.sum: Add overload for bool special case (#7975)
Browse files Browse the repository at this point in the history
Closes #7974
  • Loading branch information
AlexWaygood authored May 28, 2022
1 parent 8e8176b commit d4eba8b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...

Expand Down

0 comments on commit d4eba8b

Please sign in to comment.