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-67707: support partials in asyncio.iscoroutinefunction #94907

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions Lib/asyncio/coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ def _is_debug_mode():

def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (inspect.iscoroutinefunction(func) or
getattr(func, '_is_coroutine', None) is _is_coroutine)
return (
inspect.iscoroutinefunction(func)
or getattr(func, '_is_coroutine', None) is _is_coroutine
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this needs to both check the attribute and unwrap the partial so it can support code that worked around this issue by doing:

        def sync_fn():
            pass

        partial_sync_fn = functools.partial(sync_fn)
        partial_sync_fn._is_coroutine = asyncio.coroutines._is_coroutine

or getattr(functools._unwrap_partial(func), "_is_coroutine", None) is _is_coroutine
)


# Prioritize native coroutine check to speed-up
Expand Down
20 changes: 19 additions & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1644,9 +1644,27 @@ def fn1():
yield
self.assertFalse(asyncio.iscoroutinefunction(fn1))

async def fn2():
def fn2():
pass

fn2._is_coroutine = asyncio.coroutines._is_coroutine

self.assertTrue(asyncio.iscoroutinefunction(fn2))
self.assertTrue(asyncio.iscoroutinefunction(functools.partial(fn2)))
self.assertTrue(asyncio.iscoroutinefunction(functools.partial(functools.partial(fn2))))

async def async_fn():
pass

self.assertTrue(asyncio.iscoroutinefunction(afn2))
graingert marked this conversation as resolved.
Show resolved Hide resolved

def sync_fn():
pass

partial_sync_fn = functools.partial(sync_fn)
partial_sync_fn._is_coroutine = asyncio.coroutines._is_coroutine

self.assertTrue(asyncio.iscoroutinefunction(partial_sync_fn))

self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
Expand Down