Skip to content

Commit 894db99

Browse files
committed
Fix mocker.spy on Python 2 for non-functions
Fix #157
1 parent ca8bed5 commit 894db99

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

CHANGELOG.rst

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
1.11.1
2+
------
3+
4+
* Fix ``mocker.spy`` on Python 2 when used on non-function objects
5+
which implement ``__call__`` (`#157`_). Thanks `@pbasista`_ for
6+
the report.
7+
8+
.. _#157: https://github.com/pytest-dev/pytest-mock/issues/157
9+
.. _@pbasista: https://github.com/pbasista
10+
111
1.11.0
212
------
313

pytest_mock.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import unicode_literals
22

3+
import functools
34
import inspect
45
import sys
5-
from functools import wraps
66

77
import pytest
88

@@ -105,7 +105,13 @@ def spy(self, obj, name):
105105
if isinstance(value, (classmethod, staticmethod)):
106106
autospec = False
107107

108-
@wraps(method)
108+
if sys.version_info[0] == 2:
109+
assigned = [x for x in functools.WRAPPER_ASSIGNMENTS if hasattr(method, x)]
110+
w = functools.wraps(method, assigned=assigned)
111+
else:
112+
w = functools.wraps(method)
113+
114+
@w
109115
def wrapper(*args, **kwargs):
110116
r = method(*args, **kwargs)
111117
result.return_value = r

test_pytest_mock.py

+20
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,26 @@ class Foo(Base):
366366
assert spy.return_value == 20
367367

368368

369+
def test_callable_like_spy(testdir, mocker):
370+
testdir.makepyfile(
371+
uut="""
372+
class CallLike(object):
373+
def __call__(self, x):
374+
return x * 2
375+
376+
call_like = CallLike()
377+
"""
378+
)
379+
testdir.syspathinsert()
380+
381+
import uut
382+
383+
spy = mocker.spy(uut, "call_like")
384+
uut.call_like(10)
385+
spy.assert_called_once_with(10)
386+
assert spy.return_value == 20
387+
388+
369389
@contextmanager
370390
def assert_traceback():
371391
"""

0 commit comments

Comments
 (0)