I'd like to implement a generic class like this:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)
class MyClass(Generic[T[S]]):
def some_method(param: S) -> None:
pass
I've already tried the following:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)
class MyClass(Generic[S, T[S]]):
def some_method(param: S) -> None:
pass
def other_method(param: T) -> None:
pass
It works as expected with MyPy. However, when Python interpreter runs this code, it gives me the following error:
TypeError: 'TypeVar' object is not subscriptable.
As I found, this means that TypeVar has no [] operator implemented.
Does anyone have an idea on how to obtain the solution satisfying both mypy and Python interpreter?
EDIT: I have also tried the following:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass[S])
class MyClass(Generic[T]):
def some_method(param: S) -> None:
pass
def other_method(param: T) -> None:
pass
Python interpreter doesn't give any errors/warnings. However mypy is complaining about the second line:
Invalid type "S"