From a1bf1bfe9d98e8f200f427553265a7e564ba83c0 Mon Sep 17 00:00:00 2001 From: davfsa Date: Wed, 15 Feb 2023 22:29:05 +0100 Subject: [PATCH 1/3] Follow PEP 673 instead of PEP 484 when dealing with implicit `self` --- mypy/semanal.py | 6 +----- test-data/unit/check-selftype.test | 5 ++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index d2fd92499679..8af261797aa9 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -972,11 +972,7 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: elif isinstance(functype, CallableType): self_type = get_proper_type(functype.arg_types[0]) if isinstance(self_type, AnyType): - if has_self_type: - assert self.type is not None and self.type.self_type is not None - leading_type: Type = self.type.self_type - else: - leading_type = fill_typevars(info) + leading_type = info.self_type if info.self_type else fill_typevars(info) if func.is_class or func.name == "__new__": leading_type = self.class_type(leading_type) func.type = replace_implicit_first_type(functype, leading_type) diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index 555cef3641f8..4a3373ec6337 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -1692,9 +1692,8 @@ class C: return self def baz(self: Self) -> None: self.x = self - def bad(self) -> None: - # This is unfortunate, but required by PEP 484 - self.x = self # E: Incompatible types in assignment (expression has type "C", variable has type "Self") + def eggs(self) -> None: + self.x = self [case testTypingSelfClashInBodies] from typing import Self, TypeVar From 5d8724b3235dcbf8d690c22188033c3d57554aec Mon Sep 17 00:00:00 2001 From: davfsa Date: Wed, 15 Feb 2023 22:51:41 +0100 Subject: [PATCH 2/3] Add missing typehint --- mypy/semanal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 8af261797aa9..b08588308dc5 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -972,7 +972,7 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: elif isinstance(functype, CallableType): self_type = get_proper_type(functype.arg_types[0]) if isinstance(self_type, AnyType): - leading_type = info.self_type if info.self_type else fill_typevars(info) + leading_type: Type = info.self_type if info.self_type else fill_typevars(info) if func.is_class or func.name == "__new__": leading_type = self.class_type(leading_type) func.type = replace_implicit_first_type(functype, leading_type) From e1c12f3a9936702cb98ecef3d30303faae629325 Mon Sep 17 00:00:00 2001 From: davfsa Date: Fri, 17 Feb 2023 10:01:48 +0100 Subject: [PATCH 3/3] Test changes with `setup_self_type` --- mypy/semanal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index b08588308dc5..a465d689454f 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -972,7 +972,8 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: elif isinstance(functype, CallableType): self_type = get_proper_type(functype.arg_types[0]) if isinstance(self_type, AnyType): - leading_type: Type = info.self_type if info.self_type else fill_typevars(info) + self.setup_self_type() + leading_type: Type = info.self_type if func.is_class or func.name == "__new__": leading_type = self.class_type(leading_type) func.type = replace_implicit_first_type(functype, leading_type)