Catch more potential type errors in builtins.sum (#7578)

Use a ` TypeVar` bound to a `Protocol` defining `__add__`
This commit is contained in:
Joe Young
2022-04-01 23:19:05 +01:00
committed by GitHub
parent 646993c211
commit d45c9084a4

View File

@@ -1477,16 +1477,23 @@ def sorted(
) -> list[SupportsRichComparisonT]: ...
@overload
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...
class _SupportsSum(Protocol):
def __add__(self, __x: Any) -> Any: ...
_SumT = TypeVar("_SumT", bound=_SupportsSum)
_SumS = TypeVar("_SumS", bound=_SupportsSum)
@overload
def sum(__iterable: Iterable[_T]) -> _T | Literal[0]: ...
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...
if sys.version_info >= (3, 8):
@overload
def sum(__iterable: Iterable[_T], start: _S) -> _T | _S: ...
def sum(__iterable: Iterable[_SumT], start: _SumS) -> _SumT | _SumS: ...
else:
@overload
def sum(__iterable: Iterable[_T], __start: _S) -> _T | _S: ...
def sum(__iterable: Iterable[_SumT], __start: _SumS) -> _SumT | _SumS: ...
# The argument to `vars()` has to have a `__dict__` attribute, so can't be annotated with `object`
# (A "SupportsDunderDict" protocol doesn't work)