8000 gh-124234: Improve docs for `Mock.reset_mock` by sobolevn · Pull Request #124237 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124234: Improve docs for Mock.reset_mock #124237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
43 changes: 33 additions & 10 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ the *new_callable* argument to :func:`patch`.

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

.. doctest::

>>> mock = Mock(return_value=None)
>>> mock('hello')
>>> mock.called
Expand All @@ -409,20 +411,41 @@ the *new_callable* argument to :func:`patch`.
>>> mock.called
False

.. versionchanged:: 3.6
Added two keyword-only arguments to the reset_mock function.

This can be useful where you want to make a series of assertions that
reuse the same object. Note that :meth:`reset_mock` *doesn't* clear the
reuse the same object.

*return_value* parameter when set to ``True`` resets :attr:`return_value`:

.. doctest::

>>> mock = Mock(return_value=5)
>>> mock('hello')
5
>>> mock.reset_mock(return_value=True)
>>> mock('hello') # doctest: +ELLIPSIS
<Mock name='mock()' id='...'>

*side_effect* parameter when set to ``True`` resets :attr:`side_effect`:

.. doctest::

>>> mock = Mock(side_effect=ValueError)
>>> mock('hello')
Traceback (most recent call last):
...
ValueError
>>> mock.reset_mock(side_effect=True)
>>> mock('hello') # doctest: +ELLIPSIS
<Mock name='mock()' id='...'>

Note that :meth:`reset_mock` *doesn't* clear the
:attr:`return_value`, :attr:`side_effect` or any child attributes you have
set using normal assignment by default. In case you want to reset
:attr:`return_value` or :attr:`side_effect`, then pass the corresponding
parameter as ``True``. Child mocks and the return value mock
(if any) are reset as well.
set using normal assignment by default.

.. note:: *return_value*, and *side_effect* are keyword-only
arguments.
Child mocks are reset as well.

.. versionchanged:: 3.6
Added two keyword-only arguments to the reset_mock function.

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

Expand Down
6 changes: 4 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ def __set_side_effect(self, value):
side_effect = property(__get_side_effect, __set_side_effect)


def reset_mock(self, visited=None, *, return_value=False, side_effect=False):
def reset_mock(self, visited=None, *,
return_value: bool = False,
side_effect: bool = False):
"Restore the mock object to its initial state."
if visited is None:
visited = []
Expand Down Expand Up @@ -2218,7 +2220,7 @@ def mock_add_spec(self, spec, spec_set=False):
self._mock_add_spec(spec, spec_set)
self._mock_set_magics()

def reset_mock(self, /, *args, return_value=False, **kwargs):
def reset_mock(self, /, *args, return_value: bool = False, **kwargs):
if (
return_value
and self._mock_name
Expand Down
Loading
0