Author cbelu
Recipients cbelu
Date 2017-11-28.10:23:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1511864595.09.0.213398074469.issue32153@psf.upfronthosting.co.za>
In-reply-to
Content
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__'
History
Date User Action Args
2017-11-28 10:23:15cbelusetrecipients: + cbelu
2017-11-28 10:23:15cbelusetmessageid: <1511864595.09.0.213398074469.issue32153@psf.upfronthosting.co.za>
2017-11-28 10:23:15cbelulinkissue32153 messages
2017-11-28 10:23:14cbelucreate