-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
Description
This is a follow up for #14041
This example gives an error:
from typing import Self
class C:
x: Self
def meth(self) -> None:
self.x = self # E: Incompatible types in assignment (expression has type "C", variable has type "Self")Although it is unfortunate, technically this is required by PEP 484, that says that if no annotation is given, self has a type of current class (note also we can't bind self type for self.x expression giving it type C, as this will open another unsafety). This can be avoided by using e.g. self: Self (or generally Self type anywhere in the signature).
It looks like there can be only two solutions:
- Always infer
Selftype forself. This will cause a big performance penalty and may be surprising for users who are not familiar withSelf(if they usereveal_type(self)). - Use some heuristic and only infer
Selfwhere otherwise we will have this problem. For example, inferSelfif an attribute with type containingSelfis used anywhere in a method.
I am leaning towards the latter, but didn't want to do this until we get some more experience.
mike8699, justinas-marozas, DetachHead and MarcelWilson