diff --git a/mypy/semanal.py b/mypy/semanal.py index d2fd92499679..a465d689454f 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -972,11 +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): - 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) + 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) 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