]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122858: Deprecate `asyncio.iscoroutinefunction` (#122875)
authorWulian <1055917385@qq.com>
Sun, 11 Aug 2024 16:35:51 +0000 (00:35 +0800)
committerGitHub <noreply@github.com>
Sun, 11 Aug 2024 16:35:51 +0000 (16:35 +0000)
Deprecate `asyncio.iscoroutinefunction` in favor of `inspect.iscoroutinefunction`.

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Doc/library/unittest.mock.rst
Doc/whatsnew/3.14.rst
Lib/asyncio/base_events.py
Lib/asyncio/coroutines.py
Lib/asyncio/unix_events.py
Lib/test/test_asyncio/test_pep492.py
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_unittest/testmock/testmagicmethods.py
Lib/unittest/mock.py
Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst [new file with mode: 0644]

index 757277e102d80e7b62984e41e05c52b06e02c0c8..e15f8a4e903dc5d6aab30d7f1e2e1b65ea4e2432 100644 (file)
@@ -882,7 +882,7 @@ object::
   call is an awaitable.
 
     >>> mock = AsyncMock()
-    >>> asyncio.iscoroutinefunction(mock)
+    >>> inspect.iscoroutinefunction(mock)
     True
     >>> inspect.isawaitable(mock())  # doctest: +SKIP
     True
index a7c817c47d66c6e44928181523be5f339bcb815e..27594c3dea8161db8af844017db9cc27a5c38e0a 100644 (file)
@@ -434,6 +434,11 @@ Deprecated
   :c:macro:`!isfinite` available from :file:`math.h`
   since C99.  (Contributed by Sergey B Kirpichev in :gh:`119613`.)
 
+* :func:`!asyncio.iscoroutinefunction` is deprecated
+  and will be removed in Python 3.16,
+  use :func:`inspect.iscoroutinefunction` instead.
+  (Contributed by Jiahao Li and Kumar Aditya in :gh:`122875`.)
+
 .. Add deprecations above alphabetically, not here at the end.
 
 .. include:: ../deprecations/c-api-pending-removal-in-3.15.rst
index e4a39f4d345c79088ef939218390740b46287ac0..000647f57dd9e30c5f24ce14e406bcd82ff54f08 100644 (file)
@@ -837,7 +837,7 @@ class BaseEventLoop(events.AbstractEventLoop):
 
     def _check_callback(self, callback, method):
         if (coroutines.iscoroutine(callback) or
-                coroutines.iscoroutinefunction(callback)):
+                coroutines._iscoroutinefunction(callback)):
             raise TypeError(
                 f"coroutines cannot be used with {method}()")
         if not callable(callback):
index ab4f30eb51ba2c6083f6254d631c41f0671aaf60..a51319cb72a6a9b22104c283378b17c291126092 100644 (file)
@@ -18,7 +18,16 @@ _is_coroutine = object()
 
 
 def iscoroutinefunction(func):
+    import warnings
     """Return True if func is a decorated coroutine function."""
+    warnings._deprecated("asyncio.iscoroutinefunction",
+                         f"{warnings._DEPRECATED_MSG}; "
+                         "use inspect.iscoroutinefunction() instead",
+                         remove=(3,16))
+    return _iscoroutinefunction(func)
+
+
+def _iscoroutinefunction(func):
     return (inspect.iscoroutinefunction(func) or
             getattr(func, '_is_coroutine', None) is _is_coroutine)
 
index 2796e397c0e84d8d1090927020e554b2e017939b..0227eb506c6016857978df6933cf78c8c9583765 100644 (file)
@@ -95,7 +95,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
         Raise RuntimeError if there is a problem setting up the handler.
         """
         if (coroutines.iscoroutine(callback) or
-                coroutines.iscoroutinefunction(callback)):
+                coroutines._iscoroutinefunction(callback)):
             raise TypeError("coroutines cannot be used "
                             "with add_signal_handler()")
         self._check_signal(sig)
index 033784bc7aec05777e7bd81a3075ae1753cc582e..84c5f99129585bb78beb8d96c3f0afe1ba9434a0 100644 (file)
@@ -124,10 +124,10 @@ class CoroutineTests(BaseTest):
 
         self.assertFalse(asyncio.iscoroutine(foo()))
 
-
     def test_iscoroutinefunction(self):
         async def foo(): pass
-        self.assertTrue(asyncio.iscoroutinefunction(foo))
+        with self.assertWarns(DeprecationWarning):
+            self.assertTrue(asyncio.iscoroutinefunction(foo))
 
     def test_async_def_coroutines(self):
         async def bar():
index 9b22fb942c6339fe33dae2ad3c573619233e8b68..a1013ab803348de0097326fd9e133552676d6d5e 100644 (file)
@@ -20,6 +20,7 @@ from asyncio import tasks
 from test.test_asyncio import utils as test_utils
 from test import support
 from test.support.script_helper import assert_python_ok
+from test.support.warnings_helper import ignore_warnings
 
 
 def tearDownModule():
@@ -1939,6 +1940,7 @@ class BaseTaskTests:
         self.assertFalse(task.cancelled())
         self.assertIs(task.exception(), base_exc)
 
+    @ignore_warnings(category=DeprecationWarning)
     def test_iscoroutinefunction(self):
         def fn():
             pass
@@ -1956,6 +1958,7 @@ class BaseTaskTests:
         self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
         self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
 
+    @ignore_warnings(category=DeprecationWarning)
     def test_coroutine_non_gen_function(self):
         async def func():
             return 'test'
index a4feae7e9d3b73fbbec188f0cd6ec5df7f7066ee..5ca753b8f20811f0520fe7845fa37aade092f063 100644 (file)
@@ -1,7 +1,7 @@
 import math
 import unittest
 import os
-from asyncio import iscoroutinefunction
+from inspect import iscoroutinefunction
 from unittest.mock import AsyncMock, Mock, MagicMock, _magics
 
 
index 2bbbcf40e21543e6c65d8419046329d81b04587b..480c85bed9b31ed8fd852bd1a39279c62b2e91db 100644 (file)
@@ -32,7 +32,7 @@ import pprint
 import sys
 import builtins
 import pkgutil
-from asyncio import iscoroutinefunction
+from inspect import iscoroutinefunction
 import threading
 from types import CodeType, ModuleType, MethodType
 from unittest.util import safe_repr
@@ -2456,7 +2456,7 @@ class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock):
     recognized as an async function, and the result of a call is an awaitable:
 
     >>> mock = AsyncMock()
-    >>> iscoroutinefunction(mock)
+    >>> inspect.iscoroutinefunction(mock)
     True
     >>> inspect.isawaitable(mock())
     True
diff --git a/Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst b/Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst
new file mode 100644 (file)
index 0000000..d452ad6
--- /dev/null
@@ -0,0 +1,2 @@
+Deprecate :func:`!asyncio.iscoroutinefunction` in favor of
+:func:`inspect.iscoroutinefunction`.