Skip to content

Commit ac547e6

Browse files
committed
Added initial test and docs drafts.
1 parent c43714f commit ac547e6

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Doc/library/inspect.rst

+14
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ attributes:
358358
wrapped function is a :term:`coroutine function`.
359359

360360

361+
.. function:: markcoroutinefunction(object)
362+
363+
Decorator to mark a callable as a :term:`coroutine function` if it would not
364+
otherwise be detected by :func:`iscoroutinefunction`.
365+
366+
This may be of use for sync functions that return an awaitable or objects
367+
implementing an :keyword:`async def` ``__call__``.
368+
369+
Prefer :keyword:`async def` functions or calling the function and testing
370+
the return with :func:`isawaitable` where feasible.
371+
372+
.. versionadded:: 3.12
373+
374+
361375
.. function:: iscoroutine(object)
362376

363377
Return ``True`` if the object is a :term:`coroutine` created by an

Lib/test/test_inspect.py

+19
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,25 @@ def test_iscoroutine(self):
202202
gen_coroutine_function_example))))
203203
self.assertTrue(inspect.isgenerator(gen_coro))
204204

205+
# Use subtest initially to see both failures.
206+
with self.subTest("Wrapper not recognised."):
207+
# First case: sync function returning an awaitable.
208+
async def _fn3():
209+
pass
210+
211+
def fn3():
212+
return _fn3()
213+
self.assertTrue(inspect.iscoroutinefunction(fn3))
214+
215+
with self.subTest("Awaitable instance not recongnised."):
216+
# Second case: a class with an async def __call__.
217+
# - instance is awaitable.
218+
class Cl:
219+
async def __call__(self):
220+
pass
221+
cl = Cl()
222+
self.assertTrue(inspect.iscoroutinefunction(cl))
223+
205224
self.assertFalse(
206225
inspect.iscoroutinefunction(unittest.mock.Mock()))
207226
self.assertTrue(

0 commit comments

Comments
 (0)