Skip to content
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

docs: Add references to AsyncMock in unittest.mock.patch #13681

Merged
merged 1 commit into from
Sep 9, 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 @@ -1307,8 +1307,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just async functions? What about async methods? Is there such a thing as an async __init__ or __new__?

Copy link
Contributor Author

@mariocj89 mariocj89 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what it is controlling it: https://github.com/python/cpython/blob/master/Lib/unittest/mock.py#L49 It is checking for __code__ so it seems to be only for functions at the moment.

I was wondering about that for callable class. My main concern was what if I change a function by a callable instance but it seems there is no good way to check that at the moment. asyncio.iscoroutinefunction works only for functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, doesn't seem sensible to make changes to the docs until the rest of this is bottomed out.

Copy link
Contributor Author

@mariocj89 mariocj89 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is landed already and docs are updated in some other places. The documentation for "patch" does not do what it says it does for async functions right now. Also, I am not sure at all this is going to change, why do you think so?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like correct wording to me, perhaps we will find a way to make the callable check a little more granular but there is no reason to block this PR on what might be a future change in implementation.

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 @@ -1326,8 +1328,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 @@ -1491,6 +1493,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 @@ -2275,6 +2281,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 @@ -1598,8 +1598,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 @@ -1617,8 +1618,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