literal type

Domain: Validation Typing

šŸŽÆ Motivation

Literal restricts a value to a specific set of constants. It is perfect for settings like mode: Literal["read", "write", "append"], letting type checkers catch invalid string values at analysis time.

šŸ“‹ Concrete Example

def open_file(mode: Literal["r", "w", "a"]): ... means open_file("x") is flagged by mypy as an error because "x" is not one of the allowed literals.

šŸ”— Analogy

Literal is like a multiple-choice question where only options A, B, or C are accepted. Writing ā€œDā€ is rejected before the test is graded (code is run).