8000 docs: Add references to AsyncMock in unittest.mock.patch (GH-13681) · python/cpython@eaa1b09 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaa1b09

Browse files
docs: Add references to AsyncMock in unittest.mock.patch (GH-13681)
Update the docs as patch can now return an AsyncMock if the patched object is an async function. (cherry picked from commit f5e7f39) Co-authored-by: Mario Corchero <mcorcherojim@bloomberg.net>
1 parent eb1bc48 commit eaa1b09

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
@@ -1321,8 +1321,10 @@ patch
13211321
is patched with a *new* object. When the function/with statement exits
13221322
the patch is undone.
13231323

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

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

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

1510+
.. versionchanged:: 3.8
1511+
1512+
:func:`patch` now returns an :class:`AsyncMock` if the target is an async function.
1513+
15081514

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

22912297

2298+
.. versionchanged:: 3.8
2299+
2300+
:func:`create_autospec` now returns an :class:`AsyncMock` if the target is
2301+
an async function.
2302+
2303+
22922304
ANY
22932305
~~~
22942306

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)
0