Last active
September 28, 2022 13:21
-
-
Save dmiro/03b1c23d985cfad1bfb576c0e185a189 to your computer and use it in GitHub Desktop.
add extra info to vars (python >= 3.9)
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 Annotated | |
class Pepe(object): | |
hola_quillo: Annotated[int, "hola quillo"] | |
yepale: Annotated[float, "yepa.le"] | |
todobien: float | |
def get_display_name(className, varName): | |
from typing import get_type_hints | |
hints = get_type_hints(className, include_extras=True) | |
try: | |
return hints[varName].__metadata__[0] | |
except: | |
return varName | |
name = get_display_name(Pepe, 'hola_quillo') | |
print(name) | |
name = get_display_name(Pepe, 'yepale') | |
print(name) | |
name = get_display_name(Pepe, 'todobien') | |
print(name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
hola quillo
yepa.le
todobien