File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -358,6 +358,20 @@ attributes:
358
358
wrapped function is a :term: `coroutine function `.
359
359
360
360
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
+
361
375
.. function :: iscoroutine(object)
362
376
363
377
Return ``True `` if the object is a :term: `coroutine ` created by an
Original file line number Diff line number Diff line change @@ -202,6 +202,25 @@ def test_iscoroutine(self):
202
202
gen_coroutine_function_example ))))
203
203
self .assertTrue (inspect .isgenerator (gen_coro ))
204
204
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
+
205
224
self .assertFalse (
206
225
inspect .iscoroutinefunction (unittest .mock .Mock ()))
207
226
self .assertTrue (
You can’t perform that action at this time.
0 commit comments