From d7b88cc6bf5498dff56b351a6a037779513579df Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Wed, 16 Jul 2025 15:43:03 +0200 Subject: [PATCH 1/2] use plain_self as subtitution type for self --- mypy/subtypes.py | 3 ++- test-data/unit/check-python312.test | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 428e6dec6749..42711444d1ec 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -2208,7 +2208,8 @@ def infer_variance(info: TypeInfo, i: int) -> bool: settable = False # TODO: handle settable properties with setter type different from getter. - typ = find_member(member, self_type, self_type) + plain_self = Instance(info.mro[0], []) # self-type without type variables + typ = find_member(member, self_type, plain_self) if typ: # It's okay for a method in a generic class with a contravariant type # variable to return a generic instance of the class, if it doesn't involve diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index bfd6334b5077..7da91a930b0c 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -450,6 +450,21 @@ class Contra2[T]: d1: Contra2[int] = Contra2[float]() d2: Contra2[float] = Contra2[int]() # E: Incompatible types in assignment (expression has type "Contra2[int]", variable has type "Contra2[float]") +[case testPEP695InferVariancePolymorphicMethod] +class Cov[T]: + def get(self) -> T: ... + def new[S](self: "Cov[S]", arg: list[S]) -> "Cov[S]": ... + +cov_pos: Cov[object] = Cov[int]() +cov_neg: Cov[int] = Cov[object]() # E: Incompatible types in assignment (expression has type "Cov[object]", variable has type "Cov[int]") + +class Contra[T]: + def set(self, arg: T) -> None: ... + def new[S](self: "Contra[S]", arg: list[S]) -> "Contra[S]": ... + +contra_pos: Contra[object] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[object]") +contra_neg: Contra[int] = Contra[object]() + [case testPEP695InheritInvariant] class Invariant[T]: x: T From a0b037eec1405a69528a8de531ac98324a598831 Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Wed, 16 Jul 2025 17:42:19 +0200 Subject: [PATCH 2/2] Update mypy/subtypes.py Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com> --- mypy/subtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 42711444d1ec..3c49af722880 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -2208,7 +2208,7 @@ def infer_variance(info: TypeInfo, i: int) -> bool: settable = False # TODO: handle settable properties with setter type different from getter. - plain_self = Instance(info.mro[0], []) # self-type without type variables + plain_self = fill_typevars_with_any(info) # self-type without type variables typ = find_member(member, self_type, plain_self) if typ: # It's okay for a method in a generic class with a contravariant type pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy