Isn't this use case addressed with string annotations?

class Foo:
    def parse(self: "Foo", data: bytes) -> "Foo":
        ...
        return self
 
PEP 563 should also allow for:

class Foo:
    def parse(self: Foo, data: bytes) -> Foo:
        ...
        return self

Paul

On Wed, 2021-06-02 at 11:57 +0000, gobot1234yt@gmail.com wrote:
I would like to draft a PEP for a typing.Self class which would perform similarly to rust's Self keyword in function annotations.

Examples
```python
from typing import TypeVar

F = TypeVar("F", bound="Foo")


class Foo:
    def parse(self: F, data: bytes) -> F:
        ...
        return self
```
vs
```python
from typing import Self


class Foo:
    def parse(self, data: bytes) -> Self:
        ...
        return self
```
Another example of where this could be useful is classmethods:
```python
class Foo:
    @classmethod
    def bar(cls: type[F], *args: Any, **kwargs: Any) -> F:
        return cls()
```
vs
```python
class Foo:
    @classmethod
    def bar(cls, *args: Any, **kwargs: Any) -> Self:
        return cls()
```

Implementation wise it would be a copy of NoReturn, i.e. not subscriptable, a _SpecialForm and should only be valid as a return type.

I haven't seen any discussion of the idea on the mailing list, so I wanted to run it by everyone.

Thanks,
James.
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: pbryan@anode.ca