Skip to content

[3.8] docs: Add references to AsyncMock in unittest.mock.patch (GH-13681) #15842

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 1 commit into from
Sep 10, 2019
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
20 changes: 16 additions & 4 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,10 @@ patch
is patched with a *new* object. When the function/with statement exits
the patch is undone.

If *new* is omitted, then the target is replaced with a
:class:`MagicMock`. If :func:`patch` is used as a decorator and *new* is
If *new* is omitted, then the target is replaced with an
:class:`AsyncMock` if the patched object is an async function or
a :class:`MagicMock` otherwise.
If :func:`patch` is used as a decorator and *new* is
omitted, the created mock is passed in as an extra argument to the
decorated function. If :func:`patch` is used as a context manager the created
mock is returned by the context manager.
Expand All @@ -1340,8 +1342,8 @@ patch
patch to pass in the object being mocked as the spec/spec_set object.

*new_callable* allows you to specify a different class, or callable object,
that will be called to create the *new* object. By default :class:`MagicMock` is
used.
that will be called to create the *new* object. By default :class:`AsyncMock`
is used for async functions and :class:`MagicMock` for the rest.

A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
then the mock will be created with a spec from the object being replaced.
Expand Down Expand Up @@ -1505,6 +1507,10 @@ work as expected::
...
>>> test()

.. versionchanged:: 3.8

:func:`patch` now returns an :class:`AsyncMock` if the target is an async function.


patch.object
~~~~~~~~~~~~
Expand Down Expand Up @@ -2289,6 +2295,12 @@ See :ref:`auto-speccing` for examples of how to use auto-speccing with
:func:`create_autospec` and the *autospec* argument to :func:`patch`.


.. versionchanged:: 3.8

:func:`create_autospec` now returns an :class:`AsyncMock` if the target is
an async function.


ANY
~~~

Expand Down
9 changes: 5 additions & 4 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,9 @@ def patch(
is patched with a `new` object. When the function/with statement exits
the patch is undone.

If `new` is omitted, then the target is replaced with a
`MagicMock`. If `patch` is used as a decorator and `new` is
If `new` is omitted, then the target is replaced with an
`AsyncMock if the patched object is an async function or a
`MagicMock` otherwise. If `patch` is used as a decorator and `new` is
omitted, the created mock is passed in as an extra argument to the
decorated function. If `patch` is used as a context manager the created
mock is returned by the context manager.
Expand All @@ -1650,8 +1651,8 @@ def patch(
patch to pass in the object being mocked as the spec/spec_set object.

`new_callable` allows you to specify a different class, or callable object,
that will be called to create the `new` object. By default `MagicMock` is
used.
that will be called to create the `new` object. By default `AsyncMock` is
used for async functions and `MagicMock` for the rest.

A more powerful form of `spec` is `autospec`. If you set `autospec=True`
then the mock will be created with a spec from the object being replaced.
Expand Down