Along the same lines as NoReturn, but for iterators. Here's the simplest case I could come up with:
import itertools
def f() -> int:
for x in itertools.repeat(4):
return 4
it's a toy example, but always returns, there's no way out of the loop
mypy currently flags this as:
$ mypy --version
mypy 0.641
$ mypy t.py
t.py:4: error: Missing return statement
the workaround is to do something like this:
def f() -> int:
for x in itertools.repeat(4):
return 4
+ raise AssertionError('unreachable!')
I don't really have any suggestions for how to represent such a thing, or if it would be actually useful in practice -- I do note that mypy already has ~some concept of infinite loops that could potentially be reapplied here:
# no error
def f() -> int:
while True:
for x in itertools.repeat(4):
return x
Honestly, such code seems really rare, and apart from toy examples like this, it's also hard to analyze for a human. The convention for an unreachable point in the code is assert False (which it seems mypy understands).
or if it would be actually useful in practice
yeah that was my thought as well -- hit this during advent of code fwiw :)
I've just written this "rare" code 馃槗
for delay in (10 ** i for in itertools.count(-3, 0.5)):
if give_up:
raise Timeout()
try:
return await something(...)
except Unlucky:
pass
await asyncio.sleep(delay)
@dimaqq I also think this is not rare haha. Maybe you want to upvote #7374 or make a PR :)