mypy icon indicating copy to clipboard operation
mypy copied to clipboard

function call that returns `Never` incorrectly marked as unreachable when function that's defined later with multiple argument decorator is called

Open DetachHead opened this issue 3 years ago • 2 comments

sorry for the wacky title

from typing import TypeVar, Literal
from typing_extensions import assert_never

Fn = TypeVar("Fn")


def foo(fn: Fn, value: bool = False) -> Fn:
    ...


def bar(value: Literal[True]) -> None:
    if value:
        baz()
    else:
        assert_never(value) # error: Statement is unreachable  [unreachable]


@foo
def baz() -> None:
    ...

playground

functions that return Never are not supposed to be marked as unreachable - see https://github.com/python/mypy/issues/10916#issuecomment-1073526572

DetachHead avatar Jun 30 '22 05:06 DetachHead

Looks similar to #8900

minified:

from typing import _T as T

def foo() -> None:
    return bar()
    exit()  # error: Statement is unreachable  [unreachable]

def deco(fn: T, a: bool = False) -> T: ...

@deco
def bar() -> None: ...

KotlinIsland avatar Jun 30 '22 05:06 KotlinIsland

also causes an invalid 'Expression has type "Any"' error:

from typing import TypeVar

Fn = TypeVar("Fn")

def foo(fn: Fn, value: bool = False) -> Fn:
    ...


def f() -> None:
    baz()
    try:
        ...
    except Exception as ex:  # error: Expression has type "Any"  [misc]
        pass

@foo
def baz() -> None:
    ...

playground

very related (probably the same issue?): #13167

DetachHead avatar Aug 11 '22 06:08 DetachHead