[issue32153] mock.create_autospec fails if an attribute is a partial function
Claudiu Belu
report at bugs.python.org
Tue Nov 28 05:23:15 EST 2017
New submission from Claudiu Belu <cbelu at cloudbasesolutions.com>:
If an object's attribute is a partial function, mock.create_autospec will fail while trying to copy the partial functions' details to the mocked function, as the partial function does not have the __name__ attribute.
Example:
import functools
from unittest import mock
class Proxy(object):
def __init__(self, obj):
self.obj
def __getattr__(self, name):
return functools.partial(self.__run_method, name)
def __run_method(self, name, *args, **kwargs):
return getattr(self.obj, name)(*args, **kwargs)
a = mock.Mock()
proxy = Proxy(a)
mock.create_autospec(proxy)
Output:
Traceback (most recent call last):
File "py3_mock_functools.py", line 20, in <module>
mock.create_autospec(proxy)
File "/usr/lib/python3.5/unittest/mock.py", line 2156, in create_autospec
_check_signature(spec, mock, is_type, instance)
File "/usr/lib/python3.5/unittest/mock.py", line 112, in _check_signature
_copy_func_details(func, checksig)
File "/usr/lib/python3.5/unittest/mock.py", line 117, in _copy_func_details
funcopy.__name__ = func.__name__
AttributeError: 'functools.partial' object has no attribute '__name__'
----------
components: Library (Lib)
messages: 307115
nosy: cbelu
priority: normal
severity: normal
status: open
title: mock.create_autospec fails if an attribute is a partial function
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32153>
_______________________________________
More information about the Python-bugs-list
mailing list