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

unreachable error not triggered when unreachable line calls function with NoReturn return type #10916

Closed
DetachHead opened this issue Aug 3, 2021 · 3 comments
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@DetachHead
Copy link
Contributor

from typing import NoReturn, Literal

def foo() -> NoReturn: ...
def bar() -> None: ...

def baz(value: Literal["foo"]) -> None:
    if value != "foo":
        foo() # no error, even though statement is unreachable
    if value != "foo":
        bar() # error: Statement is unreachable  [unreachable]

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-column-numbers%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=cf13f42a0c2fb1ba0642346905e3fd27

@DetachHead DetachHead added the bug mypy got something wrong label Aug 3, 2021
@KotlinIsland
Copy link
Contributor

raise statements also count as NoReturn/<nothing>:

if False:
    raise Exception("AMONGUS")  # no error 😳

@KotlinIsland
Copy link
Contributor

This looks to be the same issue as #11437

@JelleZijlstra
Copy link
Member

This is intentional, so that you can write a runtime assertion that fails if the types are wrong and the code is unexpectedly reached at runtime.

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