Typing and Generics
typing.assert_never
Asks a checker to prove a code path unreachable by requiring a bottom-typed argument. Raises AssertionError if actually called at runtime; useful for exhaustive branching. Added in 3.11.
Formal Syntax & Structure
python syntaxpython
typing.assert_never(arg, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| arg | Never | Value expected to be unreachable. |
Return Value
Never
Typing construct or runtime result as described; static typing rules are not runtime validation.
Working Example
Example Snippetpython
from typing import Literal, assert_never
def label(value: Literal['a','b']) -> str: match value: case 'a': return 'Alpha' case 'b': return 'Beta' case _: assert_never(value)print((label('a'), label('b')))