Sample code:
from typing import Self
class Foo:
a: int
def __init__(self, a: int):
self.a = a
@classmethod
def m1(cls) -> Self:
return Foo.m2()
@classmethod
def m2(cls) -> Self:
return cls(100)
print(f"m2: {Foo.m2().a}")
print(f"m1: {Foo.m1().a}")
The code seems to work fine, both m1 and m2 print 100. However, MyPY doesn't seem to know that Foo and Self are the same type:
a.py:12: error: Incompatible return value type (got "Foo", expected "Self") [return-value]
Found 1 error in 1 file (checked 1 source file)
What is the proper way to type hint this? Or is there a better way to write it?