@@ -1321,8 +1321,10 @@ patch
1321
1321
is patched with a *new * object. When the function/with statement exits
1322
1322
the patch is undone.
1323
1323
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
1326
1328
omitted, the created mock is passed in as an extra argument to the
1327
1329
decorated function. If :func: `patch ` is used as a context manager the created
1328
1330
mock is returned by the context manager.
@@ -1340,8 +1342,8 @@ patch
1340
1342
patch to pass in the object being mocked as the spec/spec_set object.
1341
1343
1342
1344
*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 .
1345
1347
1346
1348
A more powerful form of *spec * is *autospec *. If you set ``autospec=True ``
1347
1349
then the mock will be created with a spec from the object being replaced.
@@ -1505,6 +1507,10 @@ work as expected::
1505
1507
...
1506
1508
>>> test()
1507
1509
1510
+ .. versionchanged :: 3.8
1511
+
1512
+ :func: `patch ` now returns an :class: `AsyncMock ` if the target is an async function.
1513
+
1508
1514
1509
1515
patch.object
1510
1516
~~~~~~~~~~~~
@@ -2289,6 +2295,12 @@ See :ref:`auto-speccing` for examples of how to use auto-speccing with
2289
2295
:func: `create_autospec ` and the *autospec * argument to :func: `patch `.
2290
2296
2291
2297
2298
+ .. versionchanged :: 3.8
2299
+
2300
+ :func: `create_autospec ` now returns an :class: `AsyncMock ` if the target is
2301
+ an async function.
2302
+
2303
+
2292
2304
ANY
2293
2305
~~~
2294
2306
0 commit comments