generics_defaults_specialization error

Wrong number of type arguments to a generic class or type alias

When a user-defined generic class has both required (non-default) and optional (defaulted) type parameters, the minimum number of type arguments that must be supplied when subscripting the class is the count of required parameters.

Also detects when too many type arguments are supplied to a user-defined generic class (one that has no TypeVarTuple and therefore a fixed maximum arity), or to a TypeAlias that has a fixed number of free type variables.

Additionally detects when a class that has fully specialised its generic base (e.g. class Foo(Barint)) is subscripted further, since it has no free type variables.

from typing import Generic, TypeVar, TypeAlias
from typing_extensions import TypeVar as TypeVarExt

T1 = TypeVar("T1")
T2 = TypeVar("T2")
DefaultStrT = TypeVarExt("DefaultStrT", default=str)

class AllTheDefaults(Generic[T1, T2, DefaultStrT]): ...

AllTheDefaults[int]          # E — 1 arg but at least 2 required
AllTheDefaults[int, str]     # OK
AllTheDefaults[int, str, bytes]  # OK

class LinkedList(Generic[T]): ...

LinkedList[int, str]  # E — 2 args but at most 1 allowed

MyAlias: TypeAlias = LinkedList[T2]
MyAlias[int, str]  # E — 2 args but at most 1 allowed for the alias

class Foo(LinkedList[int]): ...
Foo[str]  # E — Foo has no free type variables

How to handle it

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