annotations_forward_refs error

Invalid type expression in annotation

PEP 484 requires that annotations contain valid type expressions. Only certain expression forms are valid as types:

- Names (int, str, MyClass) - Subscripts (listint, dictstr, int) - Binary-or unions (int | str) - String literals (forward references) - None - ... (Ellipsis, in Callable signatures)

The following are invalid and should be flagged:

- List literals: int, str - Dict literals: {} - Tuple literals: (int, str) - List comprehensions: int for i in range(1) - Lambda expressions (called or uncalled) - Conditional expressions: int if cond else str - Boolean binary operators: int or str, int and str - F-string literals: f"int" - Explicit function calls like eval(...) - Negative numeric literals (positive are caught by E0024) - Names that refer to module objects (import typestypes is a module, not a type) - Names that refer to unannotated literal variables (var1 = 3var1 is int, not a type)

def f(x: [int, str]): ...            # E — list literal not a type
def g(x: int if True else str): ...  # E — conditional not a type
y: {} = {}                            # E — dict literal not a type

How to handle it

Every rule is on by default — strict is the default, not a cage. You can dial annotations_forward_refs 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/annotations_forward_refs