File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -2208,7 +2208,8 @@ def infer_variance(info: TypeInfo, i: int) -> bool:
22082208 settable = False
22092209
22102210 # TODO: handle settable properties with setter type different from getter.
2211- typ = find_member (member , self_type , self_type )
2211+ plain_self = Instance (info .mro [0 ], []) # self-type without type variables
2212+ typ = find_member (member , self_type , plain_self )
22122213 if typ :
22132214 # It's okay for a method in a generic class with a contravariant type
22142215 # variable to return a generic instance of the class, if it doesn't involve
Original file line number Diff line number Diff line change @@ -450,6 +450,21 @@ class Contra2[T]:
450450d1: Contra2[int] = Contra2[float]()
451451d2: Contra2[float] = Contra2[int]() # E: Incompatible types in assignment (expression has type "Contra2[int]", variable has type "Contra2[float]")
452452
453+ [case testPEP695InferVariancePolymorphicMethod]
454+ class Cov[T]:
455+ def get(self) -> T: ...
456+ def new[S](self: "Cov[S]", arg: list[S]) -> "Cov[S]": ...
457+
458+ cov_pos: Cov[object] = Cov[int]()
459+ cov_neg: Cov[int] = Cov[object]() # E: Incompatible types in assignment (expression has type "Cov[object]", variable has type "Cov[int]")
460+
461+ class Contra[T]:
462+ def set(self, arg: T) -> None: ...
463+ def new[S](self: "Contra[S]", arg: list[S]) -> "Contra[S]": ...
464+
465+ contra_pos: Contra[object] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[object]")
466+ contra_neg: Contra[int] = Contra[object]()
467+
453468[case testPEP695InheritInvariant]
454469class Invariant[T]:
455470 x: T
You can’t perform that action at this time.
0 commit comments