Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active September 28, 2022 13:21
Show Gist options
  • Save dmiro/03b1c23d985cfad1bfb576c0e185a189 to your computer and use it in GitHub Desktop.
Save dmiro/03b1c23d985cfad1bfb576c0e185a189 to your computer and use it in GitHub Desktop.
add extra info to vars (python >= 3.9)
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)
@dmiro
Copy link
Author

dmiro commented Sep 28, 2022

output:

hola quillo
yepa.le
todobien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment