Skip to content

if MYPY guarding an always-return block v. unreachable #11380

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

Open
gsnedders opened this issue Oct 24, 2021 · 0 comments · May be fixed by #18539
Open

if MYPY guarding an always-return block v. unreachable #11380

gsnedders opened this issue Oct 24, 2021 · 0 comments · May be fixed by #18539
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@gsnedders
Copy link

Bug Report

Given a block dependent on if MYPY, if it always returns, mypy will conclude that everything following is unreachable. This shouldn't be obviously different to using an else block.

To Reproduce

from typing import Any, cast

MYPY = False

def foo(p):
    # type: (Any) -> int
    doc = p
    if MYPY:
        return cast(int, doc)
    else:
        # needs to be in else for MYPY to believe this is reachable
        return doc

def bar(p):
    # type: (Any) -> int
    doc = p
    if MYPY:
        return cast(int, doc)
    return doc

Expected Behavior

Either both output [unreachable] or both type check successfully.

Actual Behavior

foo.py:19: error: Statement is unreachable  [unreachable]

i.e.: only bar fails to typecheck

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.0
  • Operating system and version: Arch Linux
@gsnedders gsnedders added the bug mypy got something wrong label Oct 24, 2021
@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label Apr 7, 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 topic-reachability Detecting unreachable code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants