Skip to content

Commit 4c3b469

Browse files
authored
Fix mocker.spy for non-function objects in Python 2 (#160)
Fix mocker.spy for non-function objects in Python 2
2 parents ca8bed5 + 4a661d5 commit 4c3b469

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ docs/_build/
5656
# Virtual Envs
5757
.env*
5858

59-
_pytest_mock_version.py
60-
6159
# IDE
6260
.idea
61+
/src/pytest_mock/_version.py

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
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

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from io import open
22

3-
from setuptools import setup
3+
from setuptools import setup, find_packages
44

55
setup(
66
name="pytest-mock",
77
entry_points={"pytest11": ["pytest_mock = pytest_mock"]},
8-
py_modules=["pytest_mock", "_pytest_mock_version"],
8+
packages=find_packages(where="src"),
9+
package_dir={"": "src"},
910
platforms="any",
1011
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
1112
install_requires=["pytest>=2.7", 'mock;python_version<"3.0"'],
12-
use_scm_version={"write_to": "_pytest_mock_version.py"},
13+
use_scm_version={"write_to": "src/pytest_mock/_version.py"},
1314
setup_requires=["setuptools_scm"],
1415
url="https://github.com/pytest-dev/pytest-mock/",
1516
license="MIT",

src/pytest_mock/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from pytest_mock.plugin import *
2+
from pytest_mock.plugin import _get_mock_module

pytest_mock.py renamed to src/pytest_mock/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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

9-
from _pytest_mock_version import version
9+
from ._version import version
1010

1111
__version__ = version
1212

@@ -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 renamed to tests/test_pytest_mock.py

Lines changed: 20 additions & 0 deletions
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
"""

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ passenv = USER USERNAME
66
deps =
77
coverage
88
commands =
9-
coverage run --append --source=pytest_mock.py -m pytest test_pytest_mock.py
9+
coverage run --append --source={envsitepackagesdir}/pytest_mock -m pytest tests
1010

1111
[testenv:norewrite]
1212
commands =
13-
pytest test_pytest_mock.py --assert=plain
13+
pytest tests --assert=plain
1414

1515
[testenv:linting]
1616
skipsdist = True

0 commit comments

Comments
 (0)