generics_scoping error

Unbound type variable in scope

A type variable used in a type annotation must be "in scope" — i.e. it must be bound by a surrounding generic class (GenericT), PEP 695 type parameter, or function signature parameter.

Unbound usages include: - TypeVar in a local variable annotation when the function does not bind it - TypeVar in a class body attribute when the class does not include it in Generic... - Inner class reusing an outer class's TypeVar in GenericT or body annotations - TypeVar at module level in annotations - TypeAlias at class level referencing the class's own TypeVars

T = TypeVar("T")
S = TypeVar("S")

def fun(x: T) -> list[T]:
    z: list[S] = []  # E — S is not bound in this function

class Bar(Generic[T]):
    an_attr: list[S] = []  # E — S is not bound in Bar

How to handle it

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