Skip to content

Instantly share code, notes, and snippets.

@gaganpreet
Created June 28, 2022 09:38
Show Gist options
  • Save gaganpreet/5df3396ad3bb3d3802fae14596383edf to your computer and use it in GitHub Desktop.
Save gaganpreet/5df3396ad3bb3d3802fae14596383edf to your computer and use it in GitHub Desktop.
Generic sub-class resolved type hint
from typing import Generic, TypeVar, Union, get_type_hints
T = TypeVar("T", bound=Union[str, int])
class Base(Generic[T]):
value: T
def get_value(self) -> T:
return self.value
class A(Base[str]):
value = "abc"
get_type_hints(A().get_value) # {'return': ~T}
# How do I get {'return': str}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment