Just give your mock that attribute:
mock_func.__name__ = 'foo'
That's it 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>
Setting __name__
is perfectly fine; the @wraps
decorator simply copies over the __name__
attribute to the wrapper, and on function objects that attribute is normally set to a string value. It's a writable attribute on functions, in any case, and as long as you use strings function.__name__
can be set to any value.