Description
Bug Report
This is kind of a version 2.0 for #15818, as I found an edge-case where mypy
silently passes an instance of Never
.
from typing_extensions import Never
def never_returns() -> Never:
raise NotImplementedError
def g(x: str) -> str:
return x
def h() -> str:
return g(never_returns()) # no warning raised here
g(never_returns()) # no warning raised here
# only if there was code after it we would get [unreachable]-error.
(mypy-play with --strict
and --warn-unreachable
)
Expected Behavior
The above code should raise on lines containing g(never_returns())
, as instances of Never
cannot exist, and hence cannot be passed as arguments to other functions. Therefore, the only safe behavior is that functions that return Never
can only appear "on their own", their output cannot be assigned to variables (the same code as above with x=never_returns()
in the last line gives the unhelpful message "Need type annotation for "x"") or be part of an expression:
never_returns() # OK
(never_returns() for x in range(3)) # maybe OK?
x = never_returns() # not OK
g(never_returns()) # not OK
The --warn-unreachable
flag is useless when the offending code appears in the last line of a script / function definition.