← Back to index

aliases_variance.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 4
Optional: 0 / 0
1"""
2Tests generic type aliases used in class declarations for
3variance incompatibility.
4"""
5
6from typing import Generic, TypeVar, TypeAlias
7
8T = TypeVar("T")
9T_co = TypeVar("T_co", covariant=True)
10T_contra = TypeVar("T_contra", contravariant=True)
13class ClassA(Generic[T]):
14 pass
17A_Alias_1: TypeAlias = ClassA[T_co]
18A_Alias_2: TypeAlias = A_Alias_1[T_co]
20# Specialized type aliases used within a class declaration should
21# result in the same variance incompatibility errors as their
22# non-aliased counterparts.
24class ClassA_1(ClassA[T_co]): # E: incompatible variance
Expected a ty diagnostic for this line
25 ...
28class ClassA_2(A_Alias_1[T_co]): # E: incompatible variance
Expected a ty diagnostic for this line
29 ...
32class ClassA_3(A_Alias_2[T_co]): # E: incompatible variance
Expected a ty diagnostic for this line
33 ...
37class ClassB(Generic[T, T_co]):
38 pass
41B_Alias_1 = ClassB[T_co, T_contra]
44class ClassB_1(B_Alias_1[T_contra, T_co]): # E: incompatible variance
Expected a ty diagnostic for this line
45 ...