Python – How to display the original parameters of a decorator

How to display the original parameters of a decorator… here is a solution to the problem.

How to display the original parameters of a decorator

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)

I just asked another question and it was answered very well. But another different problem arises … How can I not show it when interactive is idle :

enter image description here

But to show this:

enter image description here

Solution

What you’re looking for here is functools.wraps, which is a decorator located in the functools module that ensures that signatures, names, and almost all other metadata are preserved after decoration:

from functools import wraps

def check_range(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        counter=0
        # rest as before

Related Problems and Solutions