I want a classmethod to return correct type so i declare generic type for it.
Any subclass of TrivialClass
will also satisfy the type hint.
However inside the from_int()
, cls
is going to lose access to other()
method in Pycharm.
What's the correct way to do it?
from typing import Type, TypeVar
T = TypeVar('T', bound='TrivialClass')
class TrivialClass:
# ...
@classmethod
def from_int(cls: Type[T], int_arg: int) -> T:
# no suggestion for other, since cls type is Type[T]
cls.other()
return cls(...)
@classmethod
def other(cls):
pass