generics_defaults_referential
error
Invalid TypeVar default referencing another TypeVar
PEP 696 specifies constraints on TypeVar defaults that reference other TypeVars:
1. **Ordering**: When TypeVar T2 has default=T1, T1 must appear before T2 in generic parameter list 2. **Outer scope references**: TypeVar cannot use a TypeVar from outer scope as default 3. **Bound compatibility**: When T2 has default=T1, T1's bound must be a subtype of T2's bound 4. **Constraint superset**: When T2 has default=T1 and T2 has constraints, T1's constraints must be a subset of T2's constraints
from typing import TypeVar
# Ordering violation
T2 = TypeVar("T2", default=T1) # E — T1 not defined yet
T1 = TypeVar("T1")
# Outer scope violation
class Outer:
T1 = TypeVar("T1")
class Inner:
T2 = TypeVar("T2", default=T1) # E — T1 from outer scope
# Bound compatibility violation
X1 = TypeVar("X1", bound=int)
Invalid1 = TypeVar("Invalid1", default=X1, bound=str) # E — int is not a subtype of str
# Constraint superset violation
Y1 = TypeVar("Y1", int, str)
Invalid2 = TypeVar("Invalid2", bool, complex, default=Y1) # E — {bool, complex} is not a superset of {int, str}
How to handle it
Every rule is on by default — strict is the default, not a cage. You can dial
generics_defaults_referential 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_referential