Annotating a function that doesn't return

Quick question:

How do I annotate a function that doesn’t return (except by raising an exception)?

I didn’t know this either so I looked it up in the typing docs at typing — Support for type hints — Python 3.12.0 documentation

from typing import NoReturn

def stop() -> NoReturn:
    raise RuntimeError('no way')

(As @smurfix pointed out from 3.11 onwards Never should be used, see the link above).

Ah. I should have seen that. Thanks.

NB, looking at that link: since Py3.11 Never should be used instead.

2 Likes