diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 577d5fd99e36..c60c56720fbe 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -192,6 +192,7 @@ class super: _PositiveInteger: TypeAlias = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] _NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] +_LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed class int: @overload @@ -1648,14 +1649,14 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit # 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. +# Instead, we special-case the most common examples of this: bool and literal integers. if sys.version_info >= (3, 8): @overload - def sum(__iterable: Iterable[bool], start: int = ...) -> int: ... # type: ignore[misc] + def sum(__iterable: Iterable[bool | _LiteralInteger], start: int = ...) -> int: ... # type: ignore[misc] else: @overload - def sum(__iterable: Iterable[bool], __start: int = ...) -> int: ... # type: ignore[misc] + def sum(__iterable: Iterable[bool | _LiteralInteger], __start: int = ...) -> int: ... # type: ignore[misc] @overload def sum(__iterable: Iterable[_SupportsSumNoDefaultT]) -> _SupportsSumNoDefaultT | Literal[0]: ... diff --git a/test_cases/stdlib/builtins/test_sum.py b/test_cases/stdlib/builtins/test_sum.py index db19a2ea8617..421116767f56 100644 --- a/test_cases/stdlib/builtins/test_sum.py +++ b/test_cases/stdlib/builtins/test_sum.py @@ -22,11 +22,14 @@ def __radd__(self, other: Any) -> "Baz": return Baz() +literal_list: List[Literal[0, 1]] = [0, 1, 1] + assert_type(sum([2, 4]), int) assert_type(sum([3, 5], 4), int) assert_type(sum([True, False]), int) assert_type(sum([True, False], True), int) +assert_type(sum(literal_list), int) assert_type(sum([["foo"], ["bar"]], ["baz"]), List[str])