OK, I think I've got it.
It's not clear to me though how static type checkers can infer the SubFoo type from a TypeVar that binds to Foo.
Without using TypeVars, I suppose I would naively have to write the following:
class Foo:
def method(self) -> Foo:
...
return self
class Bar(Foo):
def method(self) -> Bar:
return super().method()
With your proposal, I could eliminate such wrapping and the static type checker would infer the return value of method is Bar for the subclass:
class Foo:
def method(self) -> Self:
...
return self
class Bar(Foo):
...
On Wed, 2021-06-02 at 13:15 +0000, gobot1234yt@gmail.com wrote:
Say parse is a method that adds attributes from the class's annotations.
```python
class Foo:
bar: int
def parse(self, data: bytes) -> Foo:
for name, type in self.__annotations__.items():
value = read_chunk(data)
assert isinstance(value, type)
setattr(self, name, value)
return self
class SubFoo:
baz: str
```
If I then call reveal_type(SubFoo().parse(...)) would say that this is of type Foo, but if I use TypeVars on the self parameter it would be correct and say that the type is of SubFoo.
_______________________________________________