Skip to content

Commit 355b5aa

Browse files
authored
Fix return type annotation for patch and spy (#364)
Fix #254
1 parent 8ba6812 commit 355b5aa

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

CHANGELOG.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Releases
22
========
33

4+
UNRELEASED
5+
-------------------
6+
7+
* Updated type annotations for ``mocker.patch`` and ``mocker.spy`` (`#364`_).
8+
9+
.. _#364: https://github.com/pytest-dev/pytest-mock/pull/364
10+
411
3.10.0 (2022-10-05)
512
-------------------
613

src/pytest_mock/plugin.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727

2828
_T = TypeVar("_T")
2929

30-
if sys.version_info[:2] > (3, 7):
30+
if sys.version_info >= (3, 8):
3131
AsyncMockType = unittest.mock.AsyncMock
32+
MockType = Union[
33+
unittest.mock.MagicMock,
34+
unittest.mock.AsyncMock,
35+
unittest.mock.NonCallableMagicMock,
36+
]
3237
else:
3338
AsyncMockType = Any
39+
MockType = Union[unittest.mock.MagicMock, unittest.mock.NonCallableMagicMock]
3440

3541

3642
class PytestMockWarning(UserWarning):
@@ -112,7 +118,7 @@ def stop(self, mock: unittest.mock.MagicMock) -> None:
112118
else:
113119
raise ValueError("This mock object is not registered")
114120

115-
def spy(self, obj: object, name: str) -> unittest.mock.MagicMock:
121+
def spy(self, obj: object, name: str) -> MockType:
116122
"""
117123
Create a spy of method. It will run method normally, but it is now
118124
possible to use `mock` call features with it, like call count.
@@ -205,13 +211,13 @@ def __init__(self, patches_and_mocks, mock_module):
205211

206212
def _start_patch(
207213
self, mock_func: Any, warn_on_mock_enter: bool, *args: Any, **kwargs: Any
208-
) -> unittest.mock.MagicMock:
214+
) -> MockType:
209215
"""Patches something by calling the given function from the mock
210216
module, registering the patch to stop it later and returns the
211217
mock object resulting from the mock call.
212218
"""
213219
p = mock_func(*args, **kwargs)
214-
mocked = p.start() # type: unittest.mock.MagicMock
220+
mocked: MockType = p.start()
215221
self.__patches_and_mocks.append((p, mocked))
216222
if hasattr(mocked, "reset_mock"):
217223
# check if `mocked` is actually a mock object, as depending on autospec or target
@@ -242,7 +248,7 @@ def object(
242248
autospec: Optional[object] = None,
243249
new_callable: object = None,
244250
**kwargs: Any
245-
) -> unittest.mock.MagicMock:
251+
) -> MockType:
246252
"""API to mock.patch.object"""
247253
if new is self.DEFAULT:
248254
new = self.mock_module.DEFAULT
@@ -271,7 +277,7 @@ def context_manager(
271277
autospec: Optional[builtins.object] = None,
272278
new_callable: builtins.object = None,
273279
**kwargs: Any
274-
) -> unittest.mock.MagicMock:
280+
) -> MockType:
275281
"""This is equivalent to mock.patch.object except that the returned mock
276282
does not issue a warning when used as a context manager."""
277283
if new is self.DEFAULT:
@@ -299,7 +305,7 @@ def multiple(
299305
autospec: Optional[builtins.object] = None,
300306
new_callable: Optional[builtins.object] = None,
301307
**kwargs: Any
302-
) -> Dict[str, unittest.mock.MagicMock]:
308+
) -> Dict[str, MockType]:
303309
"""API to mock.patch.multiple"""
304310
return self._start_patch(
305311
self.mock_module.patch.multiple,
@@ -341,7 +347,7 @@ def __call__(
341347
autospec: Optional[builtins.object] = ...,
342348
new_callable: None = ...,
343349
**kwargs: Any
344-
) -> unittest.mock.MagicMock:
350+
) -> MockType:
345351
...
346352

347353
@overload

0 commit comments

Comments
 (0)