How can I copy the signature of a method from one class, and create a "proxy method" with same signature in another ?.
I am writing a RPC library in python.
The server supports remote calls to a server-side class (C).
When the client connects to the server, it should create a proxy class for C with same signatures.
When the program calls proxy instance, it should call the function with same arguments at the server.
Consider using boltons.wraps - here's an excerpt from the documentation:
boltons.funcutils.wraps(func, injected=None, **kw)
Modeled after the built-in functools.wraps(), this function is used tomake your decorator’s wrapper functions reflect the wrappedfunction’s:
Name Documentation Module Signature
The built-in functools.wraps() copies the first three, but does notcopy the signature. This version of wraps can copy the innerfunction’s signature exactly, allowing seamless usage andintrospection. Usage is identical to the built-in version:
>>> from boltons.funcutils import wraps
>>>
>>> def print_return(func):
... @wraps(func)
... def wrapper(*args, **kwargs):
... ret = func(*args, **kwargs)
... print(ret)
... return ret
... return wrapper
...
>>> @print_return
... def example():
... '''docstring'''
... return 'example return value'
>>>
>>> val = example()
example return value
>>> example.__name__
'example'
>>> example.__doc__
'docstring'
In addition, the boltons version of wraps supports modifying the outersignature based on the inner signature. By passing a list of injectedargument names, those arguments will be removed from the outerwrapper’s signature, allowing your decorator to provide arguments thataren’t passed in.
Parameters: func (function) – The callable whose attributes are tobe copied.
injected (list) – An optional list of argument names which should notappear in the new wrapper’s signature.
update_dict (bool) – Whether to copy other, non-standard attributes offunc over to the wrapper. Defaults to True.
inject_to_varkw (bool) – Ignore missing arguments when a **kwargs-typecatch-all is present. Defaults to True.
For more in-depth wrapping of functions, see the FunctionBuilder type,on which wraps was built.