The following MWE (mypy-playground):
fromtypingimportIteratordefthis_fails(numbers: Iterator[int]) ->int:
returnnext(numbers, None) or0defintermediate_is_ok(numbers: Iterator[int]) ->int:
my_number=next(numbers, None) or0returnmy_numberdefseparate_line_is_ok(numbers: Iterator[int]) ->int:
result=next(numbers, None)
returnresultor0defusing_default_directly_is_ok(numbers: Iterator[int]) ->int:
returnnext(numbers, 0)
defhaving_optional_int_with_or_is_ok(number_maybe: int|None) ->int:
returnnumber_maybeor0
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.
The following MWE (mypy-playground):
produces the following report on
master:This is incorrect as
next(numbers, None) or 0is alwaysint.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.