Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

typing.Never

Describe the bottom type, which has no possible values.

Version: Python 3.14; repository image, production runtime not attested

Syntax

def stop(reason: str) -> Never: ...

Describe the bottom type, which has no possible values.

As a return annotation it promises that normal control never returns to the caller. A checker can use it for unreachable paths, while Python does not forbid an incorrectly annotated function from returning.

The example compares an explicitly raised exception with a mistaken ordinary return.

Examples

from typing import Never

def stop(reason: str) -> Never:
    raise ValueError(reason)

def mistake() -> Never:
    return 7

try:
    stop("empty")
except ValueError as error:
    print(str(error))
print(mistake())

Result

empty
7

The example compares an explicitly raised exception with a mistaken ordinary return.

Sources