Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions Lib/asyncio/coroutines.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__all__ = 'iscoroutinefunction', 'iscoroutine'

import collections.abc
import functools
import inspect
import os
import sys
Expand All @@ -19,8 +20,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(async_fn))

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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support partials of asyncio coroutine marked functions in asyncio.iscoroutinefunction