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).