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

gh-81432: Make create_autospec generate AsyncMocks for awaitable classes #25347

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

msuozzo
Copy link
Contributor

@msuozzo msuozzo commented Apr 11, 2021

@cjw296
Copy link
Contributor

cjw296 commented Apr 11, 2021

@tirkarthi / @lisroach - thoughts?

Lib/unittest/mock.py Outdated Show resolved Hide resolved
Lib/unittest/mock.py Outdated Show resolved Hide resolved
Copy link
Contributor

@cjw296 cjw296 left a comment

@lindacorbett - not sure this is ready to merge, and it's a tricky PR related to a problem that cropped up elsewhere. I don't have time to look until later this week, but I'd like someone with a lot of familiarity with the mock module to think this through...

@bedevere-bot
Copy link

bedevere-bot commented Apr 12, 2021

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@msuozzo
Copy link
Contributor Author

msuozzo commented Apr 12, 2021

Alright this is in a functional state. I'm not too familiar with AsyncMock but I think these changes achieve the intent of the original bug+test.

@lisroach PTAL

@tirkarthi
Copy link
Member

tirkarthi commented Apr 14, 2021

I must admit the change initially looks a little complicated to review since we had several discussion over how to detect async/coroutine properly, when to return AsyncMock, etc. and just want to understand if there is any behavior change though tests pass. I will review this PR a little later as I am working on few other things. Thanks @msuozzo .

@@ -24,14 +24,15 @@


import asyncio
import collections
Copy link
Member

@tirkarthi tirkarthi Apr 24, 2021

Choose a reason for hiding this comment

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

Please use import collections.abc as it is more explicit.

def test_create_autospec_awaitable_class(self):
self.assertIsInstance(create_autospec(AwaitableClass), AsyncMock)
Copy link
Member

@tirkarthi tirkarthi Apr 24, 2021

Choose a reason for hiding this comment

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

If I understand the changes correctly this will still fail and return MagicMock

create_autospec(AwaitableClass) will return MagicMock but instantiating returned mock will return an AsyncMock. If so can we modify this to be a test self.assertIsInstance(create_autospec(AwaitableClass), MagicMock) ?

@@ -811,12 +828,10 @@ def test_assert_awaited_but_not_called(self):
self.mock.assert_awaited()
with self.assertRaises(AssertionError):
self.mock.assert_called()
with self.assertRaises(TypeError):
# You cannot await an AsyncMock, it must be a coroutine
Copy link
Member

@tirkarthi tirkarthi Apr 24, 2021

Choose a reason for hiding this comment

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

As per this comment I guess not implementing __await__ was a conscious decision. This PR adds a behavior change making AsyncMock awaitable. I will leave this to @lisroach since I forgot the reason why this was decision was made. Perhaps I would suggest opening a new issue to track this and revert changes that make AsyncMock awaitable. For reference this comment was added in ef04851 and discussion at bpo-38136 .

@tirkarthi tirkarthi requested review from voidspace and lisroach Apr 24, 2021
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jun 3, 2021
@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Aug 6, 2022
@cjw296
Copy link
Contributor

cjw296 commented Dec 27, 2022

@lisroach - ping?

@gpshead gpshead changed the title bpo-37251: Make create_autospec generate AsyncMocks for awaitable classes gh-81432: Make create_autospec generate AsyncMocks for awaitable classes Dec 27, 2022
cjw296 added a commit that referenced this pull request Dec 28, 2022
Remove skipped test.

See discussion on #25326.
Fix is apparently here, but no-one is confident to review and land: #25347.
cjw296 added a commit to cjw296/mock that referenced this pull request Dec 28, 2022
Remove skipped test.

See discussion on python/cpython#25326.
Fix is apparently here, but no-one is confident to review and land: python/cpython#25347.

Backports: 984894a9a25c0f8298565b0c0c2e1f41917e4f88
Signed-off-by: Chris Withers <chris@simplistix.co.uk>
cjw296 added a commit to testing-cabal/mock that referenced this pull request Dec 28, 2022
Remove skipped test.

See discussion on python/cpython#25326.
Fix is apparently here, but no-one is confident to review and land: python/cpython#25347.

Backports: 984894a9a25c0f8298565b0c0c2e1f41917e4f88
Signed-off-by: Chris Withers <chris@simplistix.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants