I find Instance very confusing. The type of self is an instance? Does not make sense; also a type annotation is a class, so what does it meant that the class is instance?!
The annotation with Self does not have this issue; I would interpret it as: maybe itās a way to refer to MyClass before itās done being defined, or maybe itās a reference to the actual class or subclass at runtime, like cls in a class method. I would check the docs to know.
I would say that others agree that self: Self is ugly, but why do you think you need to put self: Self as an annotation? The draft PEP doesnāt say that and in fact explicitly says the opposite, that annotating self is uncommon. You would not write self: Self as others said earlier in this thread.
Also as an example of this in the real world, Rust already has this in its type system. The method syntax is similar to Pythonās with self being the first parameter of a method. Rust is also strongly typed and requires type annotations on all arguments to a method except self because it would be redundant.
Ah, interesting. Well, the self reference of a subclass needs an annotation. Currently, this is done by using the TypeVar, but I find this TypeVar thing very ugly.
Look at this ugliness:
from typing import TypeVar
MyBaseClassT = TypeVar("MyBaseClassT", bound="MyBaseClass")
class MyBaseClass:
...
class MySubClass(MyBaseClass):
def create_copy(self: MyBaseClassT) -> MyBaseClassT:
return self
But by having this new Self type, we wonāt need this ugly TypeVar hack anymore. So like this:
from typing import Self
class MyBaseClass:
...
class MySubClass(MyBaseClass):
def create_copy(self: Self) -> Self:
return self
Base (parent) classes never annotate self, but all subclasses need to annotate self. The idea of this PEP 673 (adding a new Self type to the typing module) is to provide a better approach (better than using the TypeVar hack) to annotate the self reference of a subclass.
When postponed evaluation of annotations gets incorporated into Python (currently, we use the from __future__ import annotations hack), I hope that we will be able to annotate the self reference of a subclass by just typing out the name of its base class, hence not needing the Self type at all. Would be nice, though.
This is incorrect usage most of the time - you should always write classes to support subclassing if possible, so you should almost never return āClassNameā, but Self instead. For the same reason you should refer to the current class as self.__class__ or type(self), and use class methods and cls. You can use a looser type (like the base class) on assignment, but not for the return type.
Whatās the status of PEP 673? Iād love to see it in, Iām really happy to see Self in typing_extensions, but I donāt want it to disappear on me. The TypeVar is really a pain, especially when you have multiple classes in one file. Plus this encourages good class design. I couldnāt find a discussion on it.
I have now embraced the fact that the new Self type might be useful. It sure as hell is a better alternative to the TypeVar hack.
Now I have a question. Is the TypeVar thing going to be removed from the language if the Self type is accepted for Python 3.11 by the Steering Council?