Just give your layout to this attribute:
mock_func.__name__ = 'foo'
What is that really.
Demo:
>>> from functools import wraps >>> from mock import Mock >>> def decor(f): ... @wraps(f) ... def wrapper(*args, **kwds): ... return f(*args, **kwds) ... return wrapper ... >>> mock_func = Mock() >>> decor(mock_func) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in decor File ".../opt/lib/python2.7/functools.py", line 33, in update_wrapper setattr(wrapper, attr, getattr(wrapped, attr)) File ".../lib/python2.7/site-packages/mock.py", line 660, in __getattr__ raise AttributeError(name) AttributeError: __name__ >>> mock_func.__name__ = 'foo' >>> decor(mock_func) <function foo at 0x10c4321b8>
The __name__ setting is perfectly fine; the @wraps decorator simply copies the __name__ attribute to the shell, and in function objects this attribute is usually set to a string value. This is a record attribute for functions anyway, and as long as you use function.__name__ , you can set any value.
source share