I’m looking for the correct way to type hint this collection of generic classes with generic methods. Here is a simplified (non-working) version:
from __future__ import annotations
import typing as t
TypeT = t.TypeVar('TypeT') # generic type not bound to anything
class BaseR(t.Generic[TypeT]):
...
TypeR = t.TypeVar('TypeR', bound=BaseR) # upper bound at BaseR
TypeE = t.TypeVar('TypeE') # generic method variable
class BaseU(t.Generic[TypeR]):
def do_something(self, var: TypeE) -> TypeR[TypeE]:
...
class SpecificR(BaseR[TypeT]):
...
class SpecificU(BaseU[SpecificR]):
...
SpecificU().do_something(1) # I want this static type to show SpecificR[int]
Why does the static typing not resolve correctly?
3