@@ -1307,8 +1307,10 @@ patch
1307
1307
is patched with a *new * object. When the function/with statement exits
1308
1308
the patch is undone.
1309
1309
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
1312
1314
omitted, the created mock is passed in as an extra argument to the
1313
1315
decorated function. If :func: `patch ` is used as a context manager the created
1314
1316
mock is returned by the context manager.
@@ -1326,8 +1328,8 @@ patch
1326
1328
patch to pass in the object being mocked as the spec/spec_set object.
1327
1329
1328
1330
*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 .
1331
1333
1332
1334
A more powerful form of *spec * is *autospec *. If you set ``autospec=True ``
1333
1335
then the mock will be created with a spec from the object being replaced.
@@ -1491,6 +1493,10 @@ work as expected::
1491
1493
...
1492
1494
>>> test()
1493
1495
1496
+ .. versionchanged :: 3.8
1497
+
1498
+ :func: `patch ` now returns an :class: `AsyncMock ` if the target is an async function.
1499
+
1494
1500
1495
1501
patch.object
1496
1502
~~~~~~~~~~~~
@@ -2275,6 +2281,12 @@ See :ref:`auto-speccing` for examples of how to use auto-speccing with
2275
2281
:func: `create_autospec ` and the *autospec * argument to :func: `patch `.
2276
2282
2277
2283
2284
+ .. versionchanged :: 3.8
2285
+
2286
+ :func: `create_autospec ` now returns an :class: `AsyncMock ` if the target is
2287
+ an async function.
2288
+
2289
+
2278
2290
ANY
2279
2291
~~~
2280
2292
0 commit comments