Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,16 @@ async def async_wrapper(*args, **kwargs):
else:
wrapped = functools.update_wrapper(wrapper, method)

autospec = inspect.ismethod(method) or inspect.isfunction(method)
autospec: bool | Callable[..., Any] = inspect.ismethod(
method
) or inspect.isfunction(method)
if not autospec and inspect.isclass(obj) and callable(method):
descriptor = inspect.getattr_static(obj, name)
if hasattr(descriptor, "__get__") and not isinstance(
descriptor, staticmethod
):
# Use the function wrapper to preserve instance method binding.
autospec = wrapped

spy_obj = cast(
SpyType,
Expand Down
55 changes: 55 additions & 0 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import os
import platform
import re
Expand Down Expand Up @@ -776,6 +777,60 @@ def bar(self, arg):
calls = [mocker.call(foo, arg=10), mocker.call(other, arg=10)]
assert spy.call_args_list == calls

@pytest.mark.parametrize("cache", [functools.cache, functools.lru_cache()])
def test_cached_instance_method(
self,
mocker: MockerFixture,
cache: Callable[[Callable[..., Any]], Callable[..., Any]],
) -> None:
calls = []

class Foo:
@cache
def bar(self, arg):
calls.append((self, arg))
return arg * 2

original = Foo.bar
spy = mocker.spy(Foo, "bar")
foo = Foo()
other = Foo()
assert foo.bar(10) == 20
assert foo.bar(10) == 20
assert other.bar(arg=10) == 20
assert spy.call_args_list == [
mocker.call(foo, 10),
mocker.call(foo, 10),
mocker.call(other, arg=10),
]
assert calls == [(foo, 10), (other, 10)]
assert spy.spy_return_list == [20, 20, 20]
mocker.resetall()
with pytest.raises(TypeError):
foo.bar()
spy.assert_not_called()
assert foo.bar(10) == 20
spy.assert_called_once_with(foo, 10)
mocker.stop(spy)
assert Foo.bar is original
assert foo.bar(10) == 20
assert calls == [(foo, 10), (other, 10)]

@pytest.mark.parametrize("method_type", [staticmethod, classmethod])
def test_cached_static_and_class_method(
self, mocker: MockerFixture, method_type: Any
) -> None:
class Foo:
@method_type
@functools.lru_cache(maxsize=2) # noqa: B019
def bar(*args):
return args[-1] * 2

spy = mocker.spy(Foo, "bar")
assert Foo.bar(10) == 20
assert Foo().bar(10) == 20
assert spy.call_args_list == [mocker.call(10), mocker.call(10)]

def test_instance_method_subclass(self, mocker: MockerFixture) -> None:
class Base:
def bar(self, arg):
Expand Down