mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-15 16:27:08 +08:00
Catch more potential type errors in builtins.sum (#7578)
Use a ` TypeVar` bound to a `Protocol` defining `__add__`
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user