NoReturn vs. None in "void" functions - type annotations in Python 3.6 NoReturn vs. None in "void" functions - type annotations in Python 3.6 python python

NoReturn vs. None in "void" functions - type annotations in Python 3.6


NoReturn means the function never returns a value.

The function either does not terminate or always throws an exception: "The typing module provides a special type NoReturn to annotate functions that never return normally. For example, a function that unconditionally raises an exception..".

from typing import NoReturndef stop() -> NoReturn:    raise RuntimeError('no way')

That is, x = foo_None() is type-valid but suspect while x = foo_NoReturn() is invalid.

Besides never having an assignable result, NoReturn also has other implications in branch analysis: foo_NoReturn(); unreachable... There is further discussion in the 'A NoReturn type is needed #165' ticket.

In order to perform branch analysis, it is necessary to know which calls will never return normally. Examples are sys.exit (which always returns via exception) and os.exit (which never returns)..