Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexei opened this issue Nov 1, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@alexei
Copy link

alexei commented Nov 1, 2024

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 #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?

@alexei alexei added the bug mypy got something wrong label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant