You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test-data/unit/check-generic-subtyping.test
+30-5Lines changed: 30 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -946,7 +946,7 @@ x1: X1[str, int]
946
946
reveal_type(list(x1)) # N: Revealed type is "builtins.list[builtins.int]"
947
947
reveal_type([*x1]) # N: Revealed type is "builtins.list[builtins.int]"
948
948
949
-
class X2(Generic[T, U], Iterator[U], Mapping[T, U]):
949
+
class X2(Generic[T, U], Iterator[U], Mapping[T, U]): # E: Definition of "__iter__" in base class "Iterable" is incompatible with definition in base class "Iterable"
950
950
pass
951
951
952
952
x2: X2[str, int]
@@ -1017,10 +1017,7 @@ x1: X1[str, int]
1017
1017
reveal_type(iter(x1)) # N: Revealed type is "typing.Iterator[builtins.int]"
1018
1018
reveal_type({**x1}) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
1019
1019
1020
-
# Some people would expect this to raise an error, but this currently does not:
1021
-
# `Mapping` has `Iterable[U]` base class, `X2` has direct `Iterable[T]` base class.
1022
-
# It would be impossible to define correct `__iter__` method for incompatible `T` and `U`.
1023
-
class X2(Generic[T, U], Mapping[U, T], Iterable[T]):
1020
+
class X2(Generic[T, U], Mapping[U, T], Iterable[T]): # E: Definition of "__iter__" in base class "Iterable" is incompatible with definition in base class "Iterable"
1024
1021
pass
1025
1022
1026
1023
x2: X2[str, int]
@@ -1065,3 +1062,31 @@ class F(E[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatib
1065
1062
1066
1063
class G(Generic[T]): ...
1067
1064
class H(G[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type
1065
+
1066
+
[case testMultipleInheritanceCompatibleTypeVar]
1067
+
from typing import Generic, TypeVar
1068
+
1069
+
T = TypeVar("T")
1070
+
U = TypeVar("U")
1071
+
1072
+
class A(Generic[T]):
1073
+
x: T
1074
+
def fn(self, t: T) -> None: ...
1075
+
1076
+
class A2(A[T]):
1077
+
y: str
1078
+
z: str
1079
+
1080
+
class B(Generic[T]):
1081
+
x: T
1082
+
def fn(self, t: T) -> None: ...
1083
+
1084
+
class C1(A2[str], B[str]): pass
1085
+
class C2(A2[str], B[int]): pass # E: Definition of "x" in base class "A" is incompatible with definition in base class "B" \
1086
+
# E: Definition of "fn" in base class "A" is incompatible with definition in base class "B"
1087
+
class C3(A2[T], B[T]): pass
1088
+
class C4(A2[U], B[U]): pass
1089
+
class C5(A2[U], B[T]): pass # E: Definition of "x" in base class "A" is incompatible with definition in base class "B" \
1090
+
# E: Definition of "fn" in base class "A" is incompatible with definition in base class "B"
0 commit comments