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

intentionally empty generator marked as unreachable #15345

Closed
asottile opened this issue Jun 1, 2023 · 6 comments · Fixed by #15722
Closed

intentionally empty generator marked as unreachable #15345

asottile opened this issue Jun 1, 2023 · 6 comments · Fixed by #15722
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@asottile
Copy link
Contributor

asottile commented Jun 1, 2023

Bug Report

this seems to be a common pattern:

def f() -> Generator[None, None, None]:
    return
    yield

it can instead be rewritten as:

def f() -> Generator[None, None, None]:
    yield from ()

To Reproduce

see above

Expected Behavior

(no errors)

Actual Behavior

$ mypy --warn-unreachable t.py
t.py:5: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.6
@asottile asottile added the bug mypy got something wrong label Jun 1, 2023
@Hayashi-Yudai
Copy link

Uum... I couldn't reproduce this bug.

  • mypy: 1.3.0
  • with --warn-unreachable flag
  • w/o mypy.ini
  • python 3.10.5
❯ mypy --warn-unreachable t.py
Success: no issues found in 1 source file

❯ cat t.py
from typing import Generator


def f() -> Generator[None, None, None]:
    yield from ()

@asottile
Copy link
Contributor Author

asottile commented Jun 2, 2023

yes you've used the workaround.

@ikonst
Copy link
Contributor

ikonst commented Jun 2, 2023

What could be a good heuristic here? That an empty yield statement (without expression) is explicitly allowed to trail a return statement?

@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label Jun 2, 2023
@asottile
Copy link
Contributor Author

asottile commented Jun 2, 2023

I'm not sure -- I think the only case is specifically the one in the issue -- I can't think of others since if it were conditional then there'd probably be a yield in the other arm. maybe this is specifically a cutout for return; yield as the total body?

@ikonst
Copy link
Contributor

ikonst commented Jun 2, 2023

How about every function having a budget for one bare yield that doesn't get flagged?

@ikonst
Copy link
Contributor

ikonst commented Jun 7, 2023

Planning to address it on top of #15386.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code
Projects
None yet
4 participants