Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5e7f39

Browse files
mariocj89lisroach
authored andcommitted
docs: Add references to AsyncMock in unittest.mock.patch (#13681)
Update the docs as patch can now return an AsyncMock if the patched object is an async function.
1 parent fa3a38d commit f5e7f39

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Doc/library/unittest.mock.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,8 +1307,10 @@ patch
13071307
is patched with a *new* object. When the function/with statement exits
13081308
the patch is undone.
13091309

1310-
If *new* is omitted, then the target is replaced with a
1311-
:class:`MagicMock`. If :func:`patch` is used as a decorator and *new* is
1310+
If *new* is omitted, then the target is replaced with an
1311+
:class:`AsyncMock` if the patched object is an async function or
1312+
a :class:`MagicMock` otherwise.
1313+
If :func:`patch` is used as a decorator and *new* is
13121314
omitted, the created mock is passed in as an extra argument to the
13131315
decorated function. If :func:`patch` is used as a context manager the created
13141316
mock is returned by the context manager.
@@ -1326,8 +1328,8 @@ patch
13261328
patch to pass in the object being mocked as the spec/spec_set object.
13271329

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

13321334
A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
13331335
then the mock will be created with a spec from the object being replaced.
@@ -1491,6 +1493,10 @@ work as expected::
14911493
...
14921494
>>> test()
14931495

1496+
.. versionchanged:: 3.8
1497+
1498+
:func:`patch` now returns an :class:`AsyncMock` if the target is an async function.
1499+
14941500

14951501
patch.object
14961502
~~~~~~~~~~~~
@@ -2275,6 +2281,12 @@ See :ref:`auto-speccing` for examples of how to use auto-speccing with
22752281
:func:`create_autospec` and the *autospec* argument to :func:`patch`.
22762282

22772283

2284+
.. versionchanged:: 3.8
2285+
2286+
:func:`create_autospec` now returns an :class:`AsyncMock` if the target is
2287+
an async function.
2288+
2289+
22782290
ANY
22792291
~~~
22802292

Lib/unittest/mock.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,8 +1631,9 @@ def patch(
16311631
is patched with a `new` object. When the function/with statement exits
16321632
the patch is undone.
16331633
1634-
If `new` is omitted, then the target is replaced with a
1635-
`MagicMock`. If `patch` is used as a decorator and `new` is
1634+
If `new` is omitted, then the target is replaced with an
1635+
`AsyncMock if the patched object is an async function or a
1636+
`MagicMock` otherwise. If `patch` is used as a decorator and `new` is
16361637
omitted, the created mock is passed in as an extra argument to the
16371638
decorated function. If `patch` is used as a context manager the created
16381639
mock is returned by the context manager.
@@ -1650,8 +1651,8 @@ def patch(
16501651
patch to pass in the object being mocked as the spec/spec_set object.
16511652
16521653
`new_callable` allows you to specify a different class, or callable object,
1653-
that will be called to create the `new` object. By default `MagicMock` is
1654-
used.
1654+
that will be called to create the `new` object. By default `AsyncMock` is
1655+
used for async functions and `MagicMock` for the rest.
16551656
16561657
A more powerful form of `spec` is `autospec`. If you set `autospec=True`
16571658
then the mock will be created with a spec from the object being replaced.

0 commit comments

Comments
 (0)