NoReturn
doesn't just mean no return
statement
Is this the right annotation for greet
?
This sounds right: after all, the function doesn't have a return
statement. However, NoReturn
means a very specific thing.
A function f
returns NoReturn
if, in the following configuration, g()
never runs:
It means that either:
f
always runs an infinite loopf
always raises an exceptionf
always terminates the current process.
What even is the point?
With NoReturn
, static analyzers can adjust their control flow model.
For example, if you call sys.exit
, Pylance knows that the next lines will never execute:
This also affects type narrowing.