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-94912: Added marker for non-standard coroutine function detection #99247

Merged
merged 16 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 14 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@ attributes:
wrapped function is a :term:`coroutine function`.


.. function:: markcoroutinefunction(object)
carltongibson marked this conversation as resolved.
Show resolved Hide resolved

Decorator to mark a callable as a :term:`coroutine function` if it would not
otherwise be detected by :func:`iscoroutinefunction`.

This may be of use for sync functions that return an awaitable or objects
implementing an :keyword:`async def` ``__call__``.

Prefer :keyword:`async def` functions or calling the function and testing
the return with :func:`isawaitable` where feasible.

.. versionadded:: 3.12


.. function:: iscoroutine(object)

Return ``True`` if the object is a :term:`coroutine` created by an
Expand Down
14 changes: 13 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"ismodule",
"isroutine",
"istraceback",
"markcoroutinefunction",
"signature",
"stack",
"trace",
Expand Down Expand Up @@ -391,12 +392,23 @@ def isgeneratorfunction(obj):
See help(isfunction) for a list of attributes."""
return _has_code_flag(obj, CO_GENERATOR)

def markcoroutinefunction(func):
"""
Decorator to ensure callable is recognised as a coroutine function.
"""
func.__code__ = func.__code__.replace(
co_flags=func.__code__.co_flags | CO_COROUTINE
)
return func

def iscoroutinefunction(obj):
"""Return true if the object is a coroutine function.

Coroutine functions are defined with "async def" syntax.
"""
return _has_code_flag(obj, CO_COROUTINE)
return _has_code_flag(obj, CO_COROUTINE) or (
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved
not isclass(obj) and callable(obj) and _has_code_flag(obj.__call__, CO_COROUTINE)
)

def isasyncgenfunction(obj):
"""Return true if the object is an asynchronous generator function.
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ def test_iscoroutine(self):
gen_coroutine_function_example))))
self.assertTrue(inspect.isgenerator(gen_coro))

async def _fn3():
pass

@inspect.markcoroutinefunction
carltongibson marked this conversation as resolved.
Show resolved Hide resolved
def fn3():
return _fn3()

self.assertTrue(inspect.iscoroutinefunction(fn3))

class Cl:
async def __call__(self):
pass

self.assertFalse(inspect.iscoroutinefunction(Cl))
self.assertTrue(inspect.iscoroutinefunction(Cl()))

class Cl2:
@inspect.markcoroutinefunction
def __call__(self):
pass

self.assertFalse(inspect.iscoroutinefunction(Cl2))
self.assertTrue(inspect.iscoroutinefunction(Cl2()))

self.assertFalse(
inspect.iscoroutinefunction(unittest.mock.Mock()))
self.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``@markcoroutinefunction`` decorator to ``inspect`` module.
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved