classification
Title: Update MagicMock __aenter__ and __aexit__ to return AsyncMock's
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lisroach Nosy List: ezio.melotti, lisroach, matrixise, michael.foord, xtreak
Priority: normal Keywords: patch

Created on 2019-09-10 14:42 by lisroach, last changed 2019-09-30 04:22 by lisroach. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15947 merged lisroach, 2019-09-11 13:42
PR 16299 merged lisroach, 2019-09-20 16:49
Messages (3)
msg351682 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2019-09-10 14:42
After a discussion with Michael, xtreak, and Ezio we've decided to try to make the process of mocking an async context manager slightly easier.

Currently if you want to mock a context manager that is used like this:

async with cm():  # note that cm is called here
  blah

You need to mock cm as a MagicMock and set __aenter__ and __aexit__ return values directly because they do not exist on MagicMocks.


Our first step to making this easier is setting the calculated return value of MagicMock __aenter__ and __aexit__ to be AsyncMock, which will make it so you do not need to set them specifically.


A future improvement may be to create a ContextManagerMock directly that can take an async kwarg that would return a context manager with AsyncMocks for __aenter__ and __aexit__ automatically.
msg352829 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2019-09-20 04:04
New changeset 8b03f943c37e07fb2394acdcfacd066647f9b1fd by Lisa Roach in branch 'master':
bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947)
https://github.com/python/cpython/commit/8b03f943c37e07fb2394acdcfacd066647f9b1fd
msg352928 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-21 06:00
New changeset 865bb685a67798eb98dcf5f3a852e08c77792998 by Stéphane Wirtel (Lisa Roach) in branch '3.8':
[3.8] bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947) (GH-16299)
https://github.com/python/cpython/commit/865bb685a67798eb98dcf5f3a852e08c77792998
History
Date User Action Args
2019-09-30 04:22:17lisroachsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-21 06:00:07matrixisesetnosy: + matrixise
messages: + msg352928
2019-09-20 16:49:02lisroachsetpull_requests: + pull_request15885
2019-09-20 04:04:21lisroachsetmessages: + msg352829
2019-09-11 13:42:00lisroachsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request15582
2019-09-10 14:42:41lisroachcreate