Closed
Description
The following MWE (mypy-playground):
from typing import Iterator
def this_fails(numbers: Iterator[int]) -> int:
return next(numbers, None) or 0
def intermediate_is_ok(numbers: Iterator[int]) -> int:
my_number = next(numbers, None) or 0
return my_number
def separate_line_is_ok(numbers: Iterator[int]) -> int:
result = next(numbers, None)
return result or 0
def using_default_directly_is_ok(numbers: Iterator[int]) -> int:
return next(numbers, 0)
def having_optional_int_with_or_is_ok(number_maybe: int | None) -> int:
return number_maybe or 0
produces the following report on master
:
main.py:4: error: Argument 2 to "next" has incompatible type "None"; expected "int" [arg-type]
This is incorrect as next(numbers, None) or 0
is always int
.
All close variants, including assigning it to an intermediate variable, don't have this issue.
I can't think of an actual application where you would want to write code like this, but I figured it's an interesting interaction that might warrant investigation.
I couldn't find an open issue about this and I asked in Gitter.