Skip to content

Commit 51b62fb

Browse files
[3.12] pythongh-124234: Improve docs for Mock.reset_mock (pythonGH-124237) (python#124592)
pythongh-124234: Improve docs for `Mock.reset_mock` (pythonGH-124237) (cherry picked from commit 19fed6c) Co-authored-by: sobolevn <[email protected]>
1 parent fca2623 commit 51b62fb

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Doc/library/unittest.mock.rst

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ the *new_callable* argument to :func:`patch`.
397397

398398
The reset_mock method resets all the call attributes on a mock object:
399399

400+
.. doctest::
401+
400402
>>> mock = Mock(return_value=None)
401403
>>> mock('hello')
402404
>>> mock.called
@@ -405,20 +407,41 @@ the *new_callable* argument to :func:`patch`.
405407
>>> mock.called
406408
False
407409

408-
.. versionchanged:: 3.6
409-
Added two keyword-only arguments to the reset_mock function.
410-
411410
This can be useful where you want to make a series of assertions that
412-
reuse the same object. Note that :meth:`reset_mock` *doesn't* clear the
411+
reuse the same object.
412+
413+
*return_value* parameter when set to ``True`` resets :attr:`return_value`:
414+
415+
.. doctest::
416+
417+
>>> mock = Mock(return_value=5)
418+
>>> mock('hello')
419+
5
420+
>>> mock.reset_mock(return_value=True)
421+
>>> mock('hello') # doctest: +ELLIPSIS
422+
<Mock name='mock()' id='...'>
423+
424+
*side_effect* parameter when set to ``True`` resets :attr:`side_effect`:
425+
426+
.. doctest::
427+
428+
>>> mock = Mock(side_effect=ValueError)
429+
>>> mock('hello')
430+
Traceback (most recent call last):
431+
...
432+
ValueError
433+
>>> mock.reset_mock(side_effect=True)
434+
>>> mock('hello') # doctest: +ELLIPSIS
435+
<Mock name='mock()' id='...'>
436+
437+
Note that :meth:`reset_mock` *doesn't* clear the
413438
:attr:`return_value`, :attr:`side_effect` or any child attributes you have
414-
set using normal assignment by default. In case you want to reset
415-
:attr:`return_value` or :attr:`side_effect`, then pass the corresponding
416-
parameter as ``True``. Child mocks and the return value mock
417-
(if any) are reset as well.
439+
set using normal assignment by default.
418440

419-
.. note:: *return_value*, and *side_effect* are keyword-only
420-
arguments.
441+
Child mocks are reset as well.
421442

443+
.. versionchanged:: 3.6
444+
Added two keyword-only arguments to the reset_mock function.
422445

423446
.. method:: mock_add_spec(spec, spec_set=False)
424447

Lib/unittest/mock.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,9 @@ def __set_side_effect(self, value):
598598
side_effect = property(__get_side_effect, __set_side_effect)
599599

600600

601-
def reset_mock(self, visited=None, *, return_value=False, side_effect=False):
601+
def reset_mock(self, visited=None, *,
602+
return_value: bool = False,
603+
side_effect: bool = False):
602604
"Restore the mock object to its initial state."
603605
if visited is None:
604606
visited = []
@@ -2189,7 +2191,7 @@ def mock_add_spec(self, spec, spec_set=False):
21892191
self._mock_add_spec(spec, spec_set)
21902192
self._mock_set_magics()
21912193

2192-
def reset_mock(self, /, *args, return_value=False, **kwargs):
2194+
def reset_mock(self, /, *args, return_value: bool = False, **kwargs):
21932195
if (
21942196
return_value
21952197
and self._mock_name

0 commit comments

Comments
 (0)