Skip to content

Sum of an iterable of bools is considered to have a bool type #7579

Closed
@lancelote

Description

@lancelote

Consider the following code

foo = sum([True, False])
foo += 1
  • mypy v0.931 is OK with it and reports foo type as int
  • mypy v0.940 warns
Incompatible types in assignment (expression has type "int", variable has type "Union[bool, Literal[0]]")

The stub does indeed state

@overload
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...

Though it is somewhat unfortunate. I've got beaten by this after updating mypy to the latest version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions