generics_defaults_2 error

Incompatible TypeVar bound or constraint with its default

PEP 696 specifies two constraints on TypeVar defaults:

1. If both bound and default are specified, the default must be a subtype of the bound. The numeric subtype hierarchy is bool <: int <: float <: complex.

2. For constrained TypeVars, the default must be one of the constraints exactly. (Even a subtype is disallowed — float is a subtype of complex but if the constraints are float, str and the default is complex, that is an error.)

from typing import TypeVar

Ok1 = TypeVar("Ok1", bound=float, default=int)     # OK — int <: float
Invalid1 = TypeVar("Invalid1", bound=str, default=int)  # E — int is not <: str

Ok2 = TypeVar("Ok2", float, str, default=float)    # OK
Invalid2 = TypeVar("Invalid2", float, str, default=int)  # E — int not in {float, str}

How to handle it

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