python - Type hint that a function never returns -


python's new type hinting feature allows type hint function returns none...

def some_func() -> none:     pass 

... or leave return type unspecified, pep dictates should cause static analysers assume return type possible:

any function without annotations should treated having general type possible

however, how should type hint function never return? instance, correct way type hint return value of these 2 functions?

def loop_forever():     while true:         print('this function never returns because loops forever')  def always_explode():     raise exception('this function never returns because raises') 

neither specifying -> none nor leaving return type unspecified seems correct in these cases.

even though “pep 484 — type hints” standard mentioned both in question , in answer yet nobody quotes section: the noreturn type covers question.

quote:

the typing module provides special type noreturn annotate functions never return normally. example, function unconditionally raises exception:

from typing import noreturn  def stop() -> noreturn:     raise runtimeerror('no way') 

the section provides examples of wrong usages. though doesn’t cover functions endless loop, in type theory both equally satisfy never returns meaning expressed special type.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -