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.
Activity
PascalPuchtler commented on Jul 25, 2023
Hey, I am new here.
I worde a *.testfile to have a running and failing test case.
This is my current version
[case testIterator] # flags: --python-version 3.10 from typing import Iterator from typing import Iterable from typing import TypeVar E = TypeVar("E") T = TypeVar("T") def next(x: Iterable[T], default: E) -> T | E: ... 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 0The test execution shows the same error as above
main:11: error: Argument 2 to "next" has incompatible type "None"; expected "int" (diff)
Can someone give me a hint, where i can find the implementation method next and or to fix this bug?
Extend special case for context-based typevar inference to typevar un…