Typing and Generics

typing.Literal

Restricts a static annotation to specific literal values such as strings, integers or enum members. The runtime does not enforce membership; check external input explicitly.

Formal Syntax & Structure

python syntaxpython
typing.Literal[value, ...]

Return Value

type expression

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Literal
def mode(value: Literal['read', 'write']) -> str: return value.upper()print(mode('read'))
Python Reference