protocols_variance_2 error

Protocol TypeVar variance mismatch

When a generic protocol class declares a TypeVar as invariant but the inferred variance (from method parameter and return positions) is strictly covariant or contravariant, a diagnostic is emitted recommending the more specific variance.

PEP 544 specifies that type checkers should warn when the inferred variance of a type variable used in a protocol differs from its declared variance.

from typing import Protocol, TypeVar

T = TypeVar("T")  # invariant

class MyProto(Protocol[T]):  # E — T should be covariant
    def method(self) -> T: ...

How to handle it

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