literals_literalstring error

LiteralString and Literal assignment incompatibilities

Detects annotated local variables inside function bodies where the declared type is incompatible with the assigned value, specifically for LiteralString and Literal... types.

Covered cases:

1. Assigning a Literal"X"-typed parameter to a Literal"Y" variable where the literal values differ. 2. Assigning an f-string containing non-LiteralString interpolations to a LiteralString-annotated variable. 3. Assigning a generic parameterised with str where LiteralString is required (invariant generics like list, Container). 4. Assigning a listLiteralString to liststr — lists are invariant.

def func(b: Literal["two"], non_literal: str):
    x1: Literal[""] = b                          # E — different literal values
    x2: LiteralString = f"{non_literal}"          # E — non-literal in f-string
    x3: Container[LiteralString] = Container(s)   # E — str ≠ LiteralString
    x4: list[str] = val                            # E — invariant mismatch

How to handle it

Every rule is on by default — strict is the default, not a cage. You can dial literals_literalstring down per-file or per-path from your editor or pyproject.toml, or fix the code so it type-checks. See the Type System rules and the complete diagnostic reference.

Canonical URL: https://www.basilisk-python.dev/errors/literals_literalstring