- Check out Self——it's really easy, isn't it?
- How about challenging yourself by using
TypeVar
instead?
Complete code following the instructions, so that lines followed by
# expect-type-error
(if any) fail type check, while others can pass.
Hit the "▶️ Run" button to see result.
"""
TODO:
`return_self` should return an instance of the same type as the current enclosed class.
"""
import typing
class Foo:
def return_self(self):
...
Stuck? Check out solution
xxxxxxxxxx
class SubclassOfFoo(Foo):
pass
f: Foo = Foo().return_self()
sf: SubclassOfFoo = SubclassOfFoo().return_self()
sf: SubclassOfFoo = Foo().return_self() # expect-type-error