Skip to content

False positive unreachable for generator that might not yield #12587

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

Closed
AckslD opened this issue Apr 14, 2022 · 4 comments
Closed

False positive unreachable for generator that might not yield #12587

AckslD opened this issue Apr 14, 2022 · 4 comments
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@AckslD
Copy link

AckslD commented Apr 14, 2022

Bug Report
(Maybe related to #8766)

Consider the following code:

from typing import Iterator


def gen() -> Iterator[int]:
    if 1 == 2:
        yield 0


def test() -> None:
    x = None
    for x in gen():
        pass
    if x is None:
        print('reachable')

To Reproduce

mypy above_file.py --warn-unreachable

Expected Behavior
No error.

Actual Behavior

tmp.py:14: error: Statement is unreachable
            print('reachable')
            ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: --warn-unreachable
  • Python version used: Python 3.9.10
  • Operating system and version: arch 5.17.1-arch1-1
@AckslD AckslD added the bug mypy got something wrong label Apr 14, 2022
@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label Apr 14, 2022
@JelleZijlstra
Copy link
Member

I think there's an existing issue for this: the problem is essentially that mypy thinks for loops always execute at least once.

@AlexWaygood
Copy link
Member

I think there's an existing issue for this

#8637?

@JelleZijlstra
Copy link
Member

Yes, thanks!

@AckslD
Copy link
Author

AckslD commented Apr 14, 2022

Thanks both!

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
Development

No branches or pull requests

3 participants