Skip to content

sum stub too permissive #7574

@jpy-git

Description

@jpy-git

Currently the stub for the sum built-in allows any iterable as an argument

typeshed/stdlib/builtins.pyi

Lines 1480 to 1489 in ae6ff79

@overload
def sum(__iterable: Iterable[_T]) -> _T | Literal[0]: ...
if sys.version_info >= (3, 8):
@overload
def sum(__iterable: Iterable[_T], start: _S) -> _T | _S: ...
else:
@overload
def sum(__iterable: Iterable[_T], __start: _S) -> _T | _S: ...

This means mypy has no issue with:

sum("hello")

Which results in this error at runtime:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

context: I was introducing someone to mypy and used the example above as something that mypy should flag, but turns out it wasn't being flagged so logging here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false negativeType checkers do not report an error, but should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions