GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-94924: support `inspect.iscoroutinefunction` in `create_autospec(async_def)` by graingert · Pull Request #94962 · python/cpython

gh-94924: support inspect.iscoroutinefunction in create_autospec(async_def) #94962

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.

Sign up for GitHub

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

Merged
cjw296 merged 6 commits into python:main from graingert:mock-create-autospec-async-def
Jun 9, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-94924: support inspect.iscoroutinefunction in create_autospec(async_def) #94962

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
1a39312
support inspect.iscoroutinefunction in create_autospec(async_def)
graingert Jul 17, 2022
0596416
test create_autospec with inspect.iscoroutine and inspect.iscoroutine…
graingert Jul 18, 2022
4ab38df
test when create_autospec functions check their signature
graingert Jul 18, 2022
034f947
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 18, 2022
79f6eb7
Merge branch 'main' into mock-create-autospec-async-def
graingert Sep 12, 2022
c521958
Merge branch 'main' into mock-create-autospec-async-def
cjw296 Jun 9, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

23 changes: 23 additions & 0 deletions 23 Lib/test/test_unittest/testmock/testasync.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ async def main():
run(main())

self.assertTrue(iscoroutinefunction(spec))
self.assertTrue(inspect.iscoroutinefunction(spec))
self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable))
self.assertEqual(spec.await_count, 1)
self.assertEqual(spec.await_args, call(1, 2, c=3))
self.assertEqual(spec.await_args_list, [call(1, 2, c=3)])
Expand All @@ -244,6 +246,25 @@ async def main():
with self.assertRaises(AssertionError):
spec.assert_any_await(e=1)

def test_autospec_checks_signature(self):
spec = create_autospec(async_func_args)
# signature is not checked when called
awaitable = spec()
self.assertListEqual(spec.mock_calls, [])

async def main():
await awaitable

# but it is checked when awaited
with self.assertRaises(TypeError):
run(main())

# _checksig_ raises before running or awaiting the mock
self.assertListEqual(spec.mock_calls, [])
self.assertEqual(spec.await_count, 0)
self.assertIsNone(spec.await_args)
self.assertEqual(spec.await_args_list, [])
spec.assert_not_awaited()

def test_patch_with_autospec(self):

Expand All @@ -253,7 +274,9 @@ async def test_async():
self.assertIsInstance(mock_method.mock, AsyncMock)

self.assertTrue(iscoroutinefunction(mock_method))
self.assertTrue(inspect.iscoroutinefunction(mock_method))
self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable))
self.assertTrue(inspect.isawaitable(awaitable))

# Verify the default values during mock setup
Expand Down
32 changes: 30 additions & 2 deletions 32 Lib/unittest/mock.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ def checksig(*args, **kwargs):
_setup_func(funcopy, mock, sig)
return funcopy

def _set_async_signature(mock, original, instance=False, is_async_mock=False):
# creates an async function with signature (*args, **kwargs) that delegates to a
# mock. It still does signature checking by calling a lambda with the same
# signature as the original.

skipfirst = isinstance(original, type)
result = _get_signature_object(original, instance, skipfirst)
if result is None:
return mock
Comment thread
cjw296 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

func, sig = result
def checksig(*args, **kwargs):
sig.bind(*args, **kwargs)
_copy_func_details(func, checksig)

name = original.__name__
if not name.isidentifier():
name = 'funcopy'
Comment thread
cjw296 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

context = {'_checksig_': checksig, 'mock': mock}
src = """async def %s(*args, **kwargs):
_checksig_(*args, **kwargs)
return await mock(*args, **kwargs)""" % name
exec (src, context)
funcopy = context[name]
_setup_func(funcopy, mock, sig)
_setup_async_mock(funcopy)
return funcopy


def _setup_func(funcopy, mock, sig):
funcopy.mock = mock
Expand Down Expand Up @@ -2745,9 +2772,10 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if isinstance(spec, FunctionTypes):
# should only happen at the top level because we don't
# recurse for functions
mock = _set_signature(mock, spec)
if is_async_func:
_setup_async_mock(mock)
mock = _set_async_signature(mock, spec)
else:
mock = _set_signature(mock, spec)
else:
_check_signature(spec, mock, is_type, instance)

Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2022-07-18-14-20-56.gh-issue-94924.X0buz2.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`unittest.mock.create_autospec` now properly returns coroutine functions compatible with :func:`inspect.iscoroutinefunction`
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.