python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.58k stars 2.85k forks source link

mypy suggests `Iterator[Never]`, wants `Iterator[None]` as return type of contextmanager #18086

Open alexei opened 1 month ago

alexei commented 1 month ago

A superficial take on a context manager (https://mypy-play.net/?mypy=latest&python=3.12&gist=68fae9f1b14467dc3a586f2473c9c419):

from contextlib import contextmanager

@contextmanager
def foo() -> None:
    yield

results in:

main.py:4: error: Argument 1 to "contextmanager" has incompatible type "Callable[[], None]"; expected "Callable[[], Iterator[Never]]"  [arg-type]
main.py:5: error: The return type of a generator function should be "Generator" or one of its supertypes  [misc]

If I specify Iterator[Never] as suggested, it still doesn't work (https://mypy-play.net/?mypy=latest&python=3.12&gist=d644d5c880c5f04b0c54941a7cc8c4b2):

from contextlib import contextmanager
from typing import Iterator, Never

@contextmanager
def foo() -> Iterator[Never]:
    yield

results in:

main.py:7: error: Yield value expected  [misc]

Inspired by https://github.com/python/mypy/issues/3551 I switched it to None (https://mypy-play.net/?mypy=latest&python=3.12&gist=e825ee207b3fbcad373163f5618f467e):

from contextlib import contextmanager
from typing import Iterator, Never

@contextmanager
def foo() -> Iterator[None]:
    yield

And now it works:

Success: no issues found in 1 source file

How? Wasn't Never (as in "never returns") supposed to not ask for a return value?