mypy
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
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:
...
functions that return Never are not supposed to be marked as unreachable - see https://github.com/python/mypy/issues/10916#issuecomment-1073526572
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: ...
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:
...
very related (probably the same issue?): #13167