Created
June 28, 2022 09:38
-
-
Save gaganpreet/5df3396ad3bb3d3802fae14596383edf to your computer and use it in GitHub Desktop.
Generic sub-class resolved type hint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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