From 1036f01516112f77eff2397b6b8042de0e75a518 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 10:24:28 +0800 Subject: [PATCH 01/16] Deprecate :func:`asyncio.iscoroutinefunction` in favor of :func:`inspect.iscoroutinefunction`. --- Doc/whatsnew/3.14.rst | 5 +++++ Lib/asyncio/base_events.py | 4 ++-- Lib/asyncio/coroutines.py | 11 ++++++++++- Lib/test/test_asyncio/test_pep492.py | 2 ++ Lib/test/test_asyncio/test_tasks.py | 3 +++ Lib/test/test_unittest/testmock/testmagicmethods.py | 2 +- Lib/unittest/mock.py | 11 +++++------ .../2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst | 2 ++ 8 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index b975f6a4f8a931..8b7ae4440e356e 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -425,6 +425,11 @@ Deprecated :c:macro:`!isfinite` available from :file:`math.h` since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.) +* :mod:`asyncio`: :func:`asyncio.iscoroutinefunction` is deprecated + and will be removed in Python 3.16; + use :func:`inspect.iscoroutinefunction` instead. + (Contributed by Jiahao Li in :gh:`122868`.) + .. Add deprecations above alphabetically, not here at the end. .. include:: ../deprecations/c-api-pending-removal-in-3.15.rst diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index e4a39f4d345c79..844a5f31a946e1 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -836,8 +836,8 @@ def call_soon(self, callback, *args, context=None): return handle def _check_callback(self, callback, method): - if (coroutines.iscoroutine(callback) or - coroutines.iscoroutinefunction(callback)): + if (coroutines._iscoroutine(callback) or + coroutines._iscoroutinefunction(callback)): raise TypeError( f"coroutines cannot be used with {method}()") if not callable(callback): diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py index ab4f30eb51ba2c..97b8c221928525 100644 --- a/Lib/asyncio/coroutines.py +++ b/Lib/asyncio/coroutines.py @@ -1,10 +1,11 @@ -__all__ = 'iscoroutinefunction', 'iscoroutine' +__all__ = ['iscoroutinefunction', 'iscoroutine'] import collections.abc import inspect import os import sys import types +import warnings def _is_debug_mode(): @@ -19,6 +20,14 @@ def _is_debug_mode(): def iscoroutinefunction(func): """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) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index 033784bc7aec05..36a1486ba9ba9f 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -8,6 +8,7 @@ import asyncio from test.test_asyncio import utils as test_utils +from test.support.warnings_helper import ignore_warnings def tearDownModule(): @@ -125,6 +126,7 @@ def foo(): yield self.assertFalse(asyncio.iscoroutine(foo())) + @ignore_warnings(category=DeprecationWarning) def test_iscoroutinefunction(self): async def foo(): pass self.assertTrue(asyncio.iscoroutinefunction(foo)) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 9b22fb942c6339..a1013ab803348d 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -20,6 +20,7 @@ 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 @@ async def notmutch(): 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 @@ async def fn2(): 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' diff --git a/Lib/test/test_unittest/testmock/testmagicmethods.py b/Lib/test/test_unittest/testmock/testmagicmethods.py index a4feae7e9d3b73..5ca753b8f20811 100644 --- a/Lib/test/test_unittest/testmock/testmagicmethods.py +++ b/Lib/test/test_unittest/testmock/testmagicmethods.py @@ -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 diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 2bbbcf40e21543..885d4188056864 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -32,7 +32,6 @@ import sys import builtins import pkgutil -from asyncio import iscoroutinefunction import threading from types import CodeType, ModuleType, MethodType from unittest.util import safe_repr @@ -57,12 +56,12 @@ def _is_async_obj(obj): return False if hasattr(obj, '__func__'): obj = getattr(obj, '__func__') - return iscoroutinefunction(obj) or inspect.isawaitable(obj) + return inspect.iscoroutinefunction(obj) or inspect.isawaitable(obj) def _is_async_func(func): if getattr(func, '__code__', None): - return iscoroutinefunction(func) + return inspect.iscoroutinefunction(func) else: return False @@ -556,7 +555,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False, unwrapped_attr = inspect.unwrap(unwrapped_attr) except ValueError: pass - if iscoroutinefunction(unwrapped_attr): + if inspect.iscoroutinefunction(unwrapped_attr): _spec_asyncs.append(attr) spec = spec_list @@ -2456,8 +2455,8 @@ class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): recognized as an async function, and the result of a call is an awaitable: >>> mock = AsyncMock() - >>> iscoroutinefunction(mock) - True + >>> inspect.iscoroutinefunction(mock) + Trues >>> 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 index 00000000000000..dff0cb9fe8ed8f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst @@ -0,0 +1,2 @@ +Deprecate :func:`asyncio.iscoroutinefunction` in favor of +:func:`inspect.iscoroutinefunction`. From 228d2f2c7ad1290676a31105a1bf6b0d3f11d611 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 10:27:41 +0800 Subject: [PATCH 02/16] gh:`122875` --- Doc/whatsnew/3.14.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 8b7ae4440e356e..47062b28811a86 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -428,7 +428,7 @@ Deprecated * :mod:`asyncio`: :func:`asyncio.iscoroutinefunction` is deprecated and will be removed in Python 3.16; use :func:`inspect.iscoroutinefunction` instead. - (Contributed by Jiahao Li in :gh:`122868`.) + (Contributed by Jiahao Li in :gh:`122875`.) .. Add deprecations above alphabetically, not here at the end. From c831bb1ce815e9d998c098a10807ab10d31f7ba5 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 10:30:02 +0800 Subject: [PATCH 03/16] typo --- Lib/unittest/mock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 885d4188056864..fe8e0be1135345 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2456,7 +2456,7 @@ class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): >>> mock = AsyncMock() >>> inspect.iscoroutinefunction(mock) - Trues + True >>> inspect.isawaitable(mock()) True From d48ed666cacfc30216fbef2e4a5a308f8d34a318 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 10:37:33 +0800 Subject: [PATCH 04/16] fix --- Doc/whatsnew/3.14.rst | 2 +- Lib/asyncio/coroutines.py | 2 +- .../next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 47062b28811a86..11ad65ae87e7e6 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -425,7 +425,7 @@ Deprecated :c:macro:`!isfinite` available from :file:`math.h` since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.) -* :mod:`asyncio`: :func:`asyncio.iscoroutinefunction` is deprecated +* :mod:`asyncio`: :func:`asyncio.coroutines.iscoroutinefunction` is deprecated and will be removed in Python 3.16; use :func:`inspect.iscoroutinefunction` instead. (Contributed by Jiahao Li in :gh:`122875`.) diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py index 97b8c221928525..9d99c9b3d6fcb7 100644 --- a/Lib/asyncio/coroutines.py +++ b/Lib/asyncio/coroutines.py @@ -1,4 +1,4 @@ -__all__ = ['iscoroutinefunction', 'iscoroutine'] +__all__ = 'iscoroutinefunction', 'iscoroutine' import collections.abc import inspect 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 index dff0cb9fe8ed8f..bb3a00fc47eb45 100644 --- 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 @@ -1,2 +1,2 @@ -Deprecate :func:`asyncio.iscoroutinefunction` in favor of +Deprecate :func:`asyncio.coroutines.iscoroutinefunction` in favor of :func:`inspect.iscoroutinefunction`. From 7aae62f7e34a7e2bf9192b8bd5dbdfff33d1888a Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 10:55:17 +0800 Subject: [PATCH 05/16] fix --- Doc/whatsnew/3.14.rst | 2 +- Lib/asyncio/base_events.py | 2 +- Lib/unittest/mock.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 11ad65ae87e7e6..0934d307ffc8e7 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -425,7 +425,7 @@ Deprecated :c:macro:`!isfinite` available from :file:`math.h` since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.) -* :mod:`asyncio`: :func:`asyncio.coroutines.iscoroutinefunction` is deprecated +* :mod:`asyncio.coroutines`: :func:`iscoroutinefunction` is deprecated and will be removed in Python 3.16; use :func:`inspect.iscoroutinefunction` instead. (Contributed by Jiahao Li in :gh:`122875`.) diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 844a5f31a946e1..000647f57dd9e3 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -836,7 +836,7 @@ def call_soon(self, callback, *args, context=None): return handle def _check_callback(self, callback, method): - if (coroutines._iscoroutine(callback) or + if (coroutines.iscoroutine(callback) or coroutines._iscoroutinefunction(callback)): raise TypeError( f"coroutines cannot be used with {method}()") diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index fe8e0be1135345..6b6c5eaa727e3f 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2837,7 +2837,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, skipfirst = _must_skip(spec, entry, is_type) child_kwargs['_eat_self'] = skipfirst - if iscoroutinefunction(original): + if inspect.iscoroutinefunction(original): child_klass = AsyncMock else: child_klass = MagicMock From ae0b8f04a17dd745bc9ac3a96047fda26d8e1e42 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 10 Aug 2024 11:20:24 +0800 Subject: [PATCH 06/16] fix --- Doc/library/unittest.mock.rst | 2 +- Doc/whatsnew/3.14.rst | 2 +- Lib/asyncio/coroutines.py | 6 +++--- Lib/asyncio/unix_events.py | 6 +++--- Lib/unittest/mock.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 757277e102d80e..e15f8a4e903dc5 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -882,7 +882,7 @@ object:: call is an awaitable. >>> mock = AsyncMock() - >>> asyncio.iscoroutinefunction(mock) + >>> inspect.iscoroutinefunction(mock) True >>> inspect.isawaitable(mock()) # doctest: +SKIP True diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 0934d307ffc8e7..23e567317102e5 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -425,7 +425,7 @@ Deprecated :c:macro:`!isfinite` available from :file:`math.h` since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.) -* :mod:`asyncio.coroutines`: :func:`iscoroutinefunction` is deprecated +* :mod:`asyncio`: :func:`iscoroutinefunction` is deprecated and will be removed in Python 3.16; use :func:`inspect.iscoroutinefunction` instead. (Contributed by Jiahao Li in :gh:`122875`.) diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py index 9d99c9b3d6fcb7..a51319cb72a6a9 100644 --- a/Lib/asyncio/coroutines.py +++ b/Lib/asyncio/coroutines.py @@ -5,7 +5,6 @@ import os import sys import types -import warnings def _is_debug_mode(): @@ -19,11 +18,12 @@ def _is_debug_mode(): def iscoroutinefunction(func): + import warnings """Return True if func is a decorated coroutine function.""" warnings._deprecated("asyncio.iscoroutinefunction", - f"{warnings._DEPRECATED_MSG};" + f"{warnings._DEPRECATED_MSG}; " "use inspect.iscoroutinefunction() instead", - remove=(3, 16)) + remove=(3,16)) return _iscoroutinefunction(func) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 2796e397c0e84d..f84e2decbe6d8e 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -16,7 +16,6 @@ from . import base_events from . import base_subprocess from . import constants -from . import coroutines from . import events from . import exceptions from . import futures @@ -94,8 +93,9 @@ def add_signal_handler(self, sig, callback, *args): Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. """ - if (coroutines.iscoroutine(callback) or - coroutines.iscoroutinefunction(callback)): + import inspect + if (inspect.iscoroutine(callback) or + inspect.iscoroutinefunction(callback)): raise TypeError("coroutines cannot be used " "with add_signal_handler()") self._check_signal(sig) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 6b6c5eaa727e3f..a8c53c63904ceb 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2306,7 +2306,7 @@ async def _execute_mock_call(self, /, *args, **kwargs): raise StopAsyncIteration if _is_exception(result): raise result - elif iscoroutinefunction(effect): + elif inspect.iscoroutinefunction(effect): result = await effect(*args, **kwargs) else: result = effect(*args, **kwargs) @@ -2318,7 +2318,7 @@ async def _execute_mock_call(self, /, *args, **kwargs): return self.return_value if self._mock_wraps is not None: - if iscoroutinefunction(self._mock_wraps): + if inspect.iscoroutinefunction(self._mock_wraps): return await self._mock_wraps(*args, **kwargs) return self._mock_wraps(*args, **kwargs) From 62b8d3bb720b077ca03a62102ac6756a5c4d4d67 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 10 Aug 2024 14:30:54 +0530 Subject: [PATCH 07/16] Apply suggestions from code review --- Doc/whatsnew/3.14.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 23e567317102e5..b4938e060fc6a6 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -425,8 +425,8 @@ Deprecated :c:macro:`!isfinite` available from :file:`math.h` since C99. (Contributed by Sergey B Kirpichev in :gh:`119613`.) -* :mod:`asyncio`: :func:`iscoroutinefunction` is deprecated - and will be removed in Python 3.16; +* :func:`!asyncio.iscoroutinefunction` is deprecated + and will be removed in Python 3.16, use :func:`inspect.iscoroutinefunction` instead. (Contributed by Jiahao Li in :gh:`122875`.) From 750d2943a3bf04f0cf4323f9f26ef0d02d2abbb8 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sat, 10 Aug 2024 19:00:19 +0800 Subject: [PATCH 08/16] use coroutines.iscoroutine and coroutines._iscoroutinefunction --- Lib/asyncio/unix_events.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index f84e2decbe6d8e..805ead394d07f7 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -93,9 +93,8 @@ def add_signal_handler(self, sig, callback, *args): Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. """ - import inspect - if (inspect.iscoroutine(callback) or - inspect.iscoroutinefunction(callback)): + if (coroutines.iscoroutine(callback) or + coroutines._iscoroutinefunction(callback)): raise TypeError("coroutines cannot be used " "with add_signal_handler()") self._check_signal(sig) From 78655aa319ad8b5fffb7c5ff698d845cc962c4fa Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sat, 10 Aug 2024 19:01:03 +0800 Subject: [PATCH 09/16] import coroutines --- Lib/asyncio/unix_events.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 805ead394d07f7..0227eb506c6016 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -16,6 +16,7 @@ from . import base_events from . import base_subprocess from . import constants +from . import coroutines from . import events from . import exceptions from . import futures From 58013d5e24299d5990307e36dc658ea23e110293 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 10 Aug 2024 17:13:33 +0530 Subject: [PATCH 10/16] Update Misc/NEWS.d/next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst --- .../next/Library/2024-08-10-10-21-44.gh-issue-122858.ZC1rJD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index bb3a00fc47eb45..d452ad6a4f6d90 100644 --- 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 @@ -1,2 +1,2 @@ -Deprecate :func:`asyncio.coroutines.iscoroutinefunction` in favor of +Deprecate :func:`!asyncio.iscoroutinefunction` in favor of :func:`inspect.iscoroutinefunction`. From 1be0d6685fba5f3c4724b029c34fb1e019553341 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sat, 10 Aug 2024 20:03:52 +0800 Subject: [PATCH 11/16] from inspect import iscoroutinefunction --- Lib/unittest/mock.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index a8c53c63904ceb..65a24bb689b2c1 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -32,6 +32,7 @@ import sys import builtins import pkgutil +from inspect import iscoroutinefunction import threading from types import CodeType, ModuleType, MethodType from unittest.util import safe_repr @@ -56,7 +57,7 @@ def _is_async_obj(obj): return False if hasattr(obj, '__func__'): obj = getattr(obj, '__func__') - return inspect.iscoroutinefunction(obj) or inspect.isawaitable(obj) + return iscoroutinefunction(obj) or inspect.isawaitable(obj) def _is_async_func(func): @@ -555,7 +556,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False, unwrapped_attr = inspect.unwrap(unwrapped_attr) except ValueError: pass - if inspect.iscoroutinefunction(unwrapped_attr): + if iscoroutinefunction(unwrapped_attr): _spec_asyncs.append(attr) spec = spec_list @@ -2306,7 +2307,7 @@ async def _execute_mock_call(self, /, *args, **kwargs): raise StopAsyncIteration if _is_exception(result): raise result - elif inspect.iscoroutinefunction(effect): + elif iscoroutinefunction(effect): result = await effect(*args, **kwargs) else: result = effect(*args, **kwargs) @@ -2318,7 +2319,7 @@ async def _execute_mock_call(self, /, *args, **kwargs): return self.return_value if self._mock_wraps is not None: - if inspect.iscoroutinefunction(self._mock_wraps): + if iscoroutinefunction(self._mock_wraps): return await self._mock_wraps(*args, **kwargs) return self._mock_wraps(*args, **kwargs) @@ -2837,7 +2838,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, skipfirst = _must_skip(spec, entry, is_type) child_kwargs['_eat_self'] = skipfirst - if inspect.iscoroutinefunction(original): + if iscoroutinefunction(original): child_klass = AsyncMock else: child_klass = MagicMock From 840191d6c0772517e5d851dcb1690342f737ed8a Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 11 Aug 2024 07:25:25 +0800 Subject: [PATCH 12/16] remove --- Lib/unittest/mock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 65a24bb689b2c1..480c85bed9b31e 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -62,7 +62,7 @@ def _is_async_obj(obj): def _is_async_func(func): if getattr(func, '__code__', None): - return inspect.iscoroutinefunction(func) + return iscoroutinefunction(func) else: return False From e0f02727c3721d5a5c23a6b6f36b7e54ce3b8455 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 11 Aug 2024 07:28:08 +0800 Subject: [PATCH 13/16] test_pep492.py --- Lib/test/test_asyncio/test_pep492.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index 36a1486ba9ba9f..ff5f611b42cb51 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -8,7 +8,6 @@ import asyncio from test.test_asyncio import utils as test_utils -from test.support.warnings_helper import ignore_warnings def tearDownModule(): @@ -126,7 +125,7 @@ def foo(): yield self.assertFalse(asyncio.iscoroutine(foo())) - @ignore_warnings(category=DeprecationWarning) + def test_iscoroutinefunction(self): async def foo(): pass self.assertTrue(asyncio.iscoroutinefunction(foo)) From 231cfb3bb6429ae3fbecb3615e9fe8506daaa4e7 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 11 Aug 2024 07:28:57 +0800 Subject: [PATCH 14/16] finish --- Lib/test/test_asyncio/test_pep492.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index ff5f611b42cb51..033784bc7aec05 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -125,7 +125,6 @@ def foo(): yield self.assertFalse(asyncio.iscoroutine(foo())) - def test_iscoroutinefunction(self): async def foo(): pass self.assertTrue(asyncio.iscoroutinefunction(foo)) From c1f2743db688e4fa58f9d099320000a629267a8c Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 11 Aug 2024 08:10:36 +0800 Subject: [PATCH 15/16] test_pep492 --- Lib/test/test_asyncio/test_pep492.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index 033784bc7aec05..087607fbb0158d 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -8,6 +8,7 @@ import asyncio from test.test_asyncio import utils as test_utils +from test.support.warnings_helper import ignore_warnings def tearDownModule(): @@ -124,7 +125,7 @@ def foo(): yield self.assertFalse(asyncio.iscoroutine(foo())) - + @ignore_warnings(category=DeprecationWarning) def test_iscoroutinefunction(self): async def foo(): pass self.assertTrue(asyncio.iscoroutinefunction(foo)) From d8c86c6406e88cf2473a3b557b5e22d9ff2b6214 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sun, 11 Aug 2024 21:42:32 +0530 Subject: [PATCH 16/16] fix tests --- Doc/whatsnew/3.14.rst | 2 +- Lib/test/test_asyncio/test_pep492.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index b4938e060fc6a6..900d04a7de2036 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -428,7 +428,7 @@ Deprecated * :func:`!asyncio.iscoroutinefunction` is deprecated and will be removed in Python 3.16, use :func:`inspect.iscoroutinefunction` instead. - (Contributed by Jiahao Li in :gh:`122875`.) + (Contributed by Jiahao Li and Kumar Aditya in :gh:`122875`.) .. Add deprecations above alphabetically, not here at the end. diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index 087607fbb0158d..84c5f99129585b 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -8,7 +8,6 @@ import asyncio from test.test_asyncio import utils as test_utils -from test.support.warnings_helper import ignore_warnings def tearDownModule(): @@ -125,10 +124,10 @@ def foo(): yield self.assertFalse(asyncio.iscoroutine(foo())) - @ignore_warnings(category=DeprecationWarning) 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():