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

"Missing return statement" for generators that implicitly return None #13019

Closed
DanielNoord opened this issue Jun 24, 2022 · 4 comments
Closed
Labels
bug mypy got something wrong

Comments

@DanielNoord
Copy link

Bug Report

from typing import Generator
class A:
    def func(self) -> Generator[str, None, int | None]:
        for i in ("1", "2"):
            yield i

class B(A):
    def func(self) -> Generator[str, None, int]:
        for i in ("1", "2"):
            yield i
        return 1

mypy complains about Missing return statement [return] on L3. This can be satisfied with a return None on L6 after the for loop. However, since the return type of the generator is None that shouldn't really be necessary. Like for simpler functions the return of None should be implicit.

To Reproduce

Run mypy on code above.

Expected Behavior

No warning.

Your Environment

mypy 0.961 (compiled: no) on macOS.
Python version is 3.11.0b3 but this occurs on other versions as well.

@JelleZijlstra
Copy link
Member

This is the same behavior as for non-generator functions, so we're being consistent. See #7511 (and I think there was another issue more recently where people complained about this).

@DanielNoord
Copy link
Author

This is the same behavior as for non-generator functions, so we're being consistent. See #7511 (and I think there was another issue more recently where people complained about this).

Ah, I didn't know this. Is there any way to get confirmation on that issue that this is working as intended? Should that go via a discussion topic or typing-sig? It would be good to know whether we can use an ignore in the hope of removing it when this is allowed or just add the additional return if this is working as intended.

@JelleZijlstra
Copy link
Member

It's working as intended in mypy. But I don't think the general type system requires it, and other type checkers may make a different decision. And of course, we could change our mind.

@DanielNoord
Copy link
Author

I mean, the added return statement wouldn't make any of the other checkers complain so it isn't that big of an issue.

We'll probably need to update pylint to no longer warn about this though. That might also prevent some users who struggle to satisfy both mypy and pylint from complaining about this.

@DanielNoord DanielNoord closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2022
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

2 participants