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

Created on 2019-09-10 14:42 by lisroach, last changed 2019-09-11 13:42 by lisroach.

Pull Requests
URL Status Linked Edit
PR 15947 open lisroach, 2019-09-11 13:42
Messages (1)
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.
History
Date User Action Args
2019-09-11 13:42:00lisroachsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request15582
2019-09-10 14:42:41lisroachcreate