This rule raises an issue when a TypeVar is declared with both covariant=True and contravariant=True.
A TypeVar in Python represents a type variable used in generic programming. It can have one of three variance modes:
covariant=True: the type can be replaced with a subtypecontravariant=True: the type can be replaced with a supertypeThese variance modes are mutually exclusive. A type variable cannot be both covariant and contravariant simultaneously because these represent
opposite relationships in the type hierarchy. Python’s typing module will raise a ValueError at runtime when it encounters
such a declaration.
ValueError when the module is imported, causing the application to crash immediately.Choose the appropriate variance mode for your type variable:
covariant=Truecontravariant=True
from typing import TypeVar
# Invalid: cannot be both covariant and contravariant
T = TypeVar('T', covariant=True, contravariant=True) # Noncompliant
from typing import TypeVar
# Valid: covariant type variable
T_co = TypeVar('T_co', covariant=True)
TypeVar