from inspect import signature
from typing import get_type_hints
def check_range(f):
def decorated(*args, **kwargs): #something should be modified here
counter=0
# use get_type_hints instead of __annotations__
annotations = get_type_hints(f)
# bind signature to arguments and get an
# ordered dictionary of the arguments
b = signature(f).bind(*args, **kwargs).arguments
for name, value in b.items():
if not isinstance(value, annotations[name]):
msg = 'Not the valid type'
raise ValueError(msg)
counter+=1
return f(*args, **kwargs) #something should be modified here
return decorated
@check_range
def foo(a: int, b: list, c: str):
print(a,b,c)
J'ai posé une autre question il y a quelque temps et il a obtenu une réponse splendidly. Mais une autre question différente surgit ... Comment puis-je faire pour ne pas montrer ce au ralenti interactif:Comment montrer les arguments d'origine pour un décorateur
Mais pour montrer ceci:
Merci encore pour votre aide – abccd
De rien @abccd. Puisque vous effectuez un contrôle de type, je voudrais souligner que Python a un vérificateur de type * static * qui vérifie le type pour vous. C'est ce qu'on appelle "mypy", donnez un coup d'oeil si vous êtes intéressé. –
Je voudrais vraiment regarder dedans. Et merci encore :) – abccd