Skip to content

Commit bc9d92c

Browse files
gh-122858: Deprecate asyncio.iscoroutinefunction (#122875)
Deprecate `asyncio.iscoroutinefunction` in favor of `inspect.iscoroutinefunction`. Co-authored-by: Kumar Aditya <[email protected]>
1 parent 3aaed08 commit bc9d92c

File tree

10 files changed

+27
-8
lines changed

10 files changed

+27
-8
lines changed

Doc/library/unittest.mock.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ object::
882882
call is an awaitable.
883883

884884
>>> mock = AsyncMock()
885-
>>> asyncio.iscoroutinefunction(mock)
885+
>>> inspect.iscoroutinefunction(mock)
886886
True
887887
>>> inspect.isawaitable(mock()) # doctest: +SKIP
888888
True

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ Deprecated
434434
:c:macro:`!isfinite` available from :file:`math.h`
435435
since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.)
436436

437+
* :func:`!asyncio.iscoroutinefunction` is deprecated
438+
and will be removed in Python 3.16,
439+
use :func:`inspect.iscoroutinefunction` instead.
440+
(Contributed by Jiahao Li and Kumar Aditya in :gh:`122875`.)
441+
437442
.. Add deprecations above alphabetically, not here at the end.
438443
439444
.. include:: ../deprecations/c-api-pending-removal-in-3.15.rst

Lib/asyncio/base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def call_soon(self, callback, *args, context=None):
837837

838838
def _check_callback(self, callback, method):
839839
if (coroutines.iscoroutine(callback) or
840-
coroutines.iscoroutinefunction(callback)):
840+
coroutines._iscoroutinefunction(callback)):
841841
raise TypeError(
842842
f"coroutines cannot be used with {method}()")
843843
if not callable(callback):

Lib/asyncio/coroutines.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ def _is_debug_mode():
1818

1919

2020
def iscoroutinefunction(func):
21+
import warnings
2122
"""Return True if func is a decorated coroutine function."""
23+
warnings._deprecated("asyncio.iscoroutinefunction",
24+
f"{warnings._DEPRECATED_MSG}; "
25+
"use inspect.iscoroutinefunction() instead",
26+
remove=(3,16))
27+
return _iscoroutinefunction(func)
28+
29+
30+
def _iscoroutinefunction(func):
2231
return (inspect.iscoroutinefunction(func) or
2332
getattr(func, '_is_coroutine', None) is _is_coroutine)
2433

Lib/asyncio/unix_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def add_signal_handler(self, sig, callback, *args):
9595
Raise RuntimeError if there is a problem setting up the handler.
9696
"""
9797
if (coroutines.iscoroutine(callback) or
98-
coroutines.iscoroutinefunction(callback)):
98+
coroutines._iscoroutinefunction(callback)):
9999
raise TypeError("coroutines cannot be used "
100100
"with add_signal_handler()")
101101
self._check_signal(sig)

Lib/test/test_asyncio/test_pep492.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ def foo(): yield
124124

125125
self.assertFalse(asyncio.iscoroutine(foo()))
126126

127-
128127
def test_iscoroutinefunction(self):
129128
async def foo(): pass
130-
self.assertTrue(asyncio.iscoroutinefunction(foo))
129+
with self.assertWarns(DeprecationWarning):
130+
self.assertTrue(asyncio.iscoroutinefunction(foo))
131131

132132
def test_async_def_coroutines(self):
133133
async def bar():

Lib/test/test_asyncio/test_tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from test.test_asyncio import utils as test_utils
2121
from test import support
2222
from test.support.script_helper import assert_python_ok
23+
from test.support.warnings_helper import ignore_warnings
2324

2425

2526
def tearDownModule():
@@ -1939,6 +1940,7 @@ async def notmutch():
19391940
self.assertFalse(task.cancelled())
19401941
self.assertIs(task.exception(), base_exc)
19411942

1943+
@ignore_warnings(category=DeprecationWarning)
19421944
def test_iscoroutinefunction(self):
19431945
def fn():
19441946
pass
@@ -1956,6 +1958,7 @@ async def fn2():
19561958
self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
19571959
self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
19581960

1961+
@ignore_warnings(category=DeprecationWarning)
19591962
def test_coroutine_non_gen_function(self):
19601963
async def func():
19611964
return 'test'

Lib/test/test_unittest/testmock/testmagicmethods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import math
22
import unittest
33
import os
4-
from asyncio import iscoroutinefunction
4+
from inspect import iscoroutinefunction
55
from unittest.mock import AsyncMock, Mock, MagicMock, _magics
66

77

Lib/unittest/mock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import sys
3333
import builtins
3434
import pkgutil
35-
from asyncio import iscoroutinefunction
35+
from inspect import iscoroutinefunction
3636
import threading
3737
from types import CodeType, ModuleType, MethodType
3838
from unittest.util import safe_repr
@@ -2456,7 +2456,7 @@ class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock):
24562456
recognized as an async function, and the result of a call is an awaitable:
24572457
24582458
>>> mock = AsyncMock()
2459-
>>> iscoroutinefunction(mock)
2459+
>>> inspect.iscoroutinefunction(mock)
24602460
True
24612461
>>> inspect.isawaitable(mock())
24622462
True
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate :func:`!asyncio.iscoroutinefunction` in favor of
2+
:func:`inspect.iscoroutinefunction`.

0 commit comments

Comments
 (0)