Skip to content

Commit d7b88cc

Browse files
use plain_self as subtitution type for self
1 parent 6403752 commit d7b88cc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

test-data/unit/check-python312.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,21 @@ class Contra2[T]:
450450
d1: Contra2[int] = Contra2[float]()
451451
d2: 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]
454469
class Invariant[T]:
455470
x: T

0 commit comments

Comments
 (0)