]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-122088: Copy the coroutine status of the underlying callable in `@warnings...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 23 Jul 2024 10:24:07 +0000 (12:24 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Jul 2024 10:24:07 +0000 (10:24 +0000)
Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/test/test_warnings/__init__.py
Lib/warnings.py
Misc/NEWS.d/next/Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst [new file with mode: 0644]

index 4416ed0f3ed3ef98f861e3f66079af8a7f1f60e9..e7a6f732ee10e8a74aec76bceb75746abd4b5614 100644 (file)
@@ -1,6 +1,7 @@
 from contextlib import contextmanager
 import linecache
 import os
+import inspect
 from io import StringIO
 import re
 import sys
@@ -1682,6 +1683,29 @@ class DeprecatedTests(unittest.TestCase):
             isinstance(cell.cell_contents, deprecated) for cell in d.__closure__
         ))
 
+    def test_inspect(self):
+        @deprecated("depr")
+        def sync():
+            pass
+
+        @deprecated("depr")
+        async def coro():
+            pass
+
+        class Cls:
+            @deprecated("depr")
+            def sync(self):
+                pass
+
+            @deprecated("depr")
+            async def coro(self):
+                pass
+
+        self.assertFalse(inspect.iscoroutinefunction(sync))
+        self.assertTrue(inspect.iscoroutinefunction(coro))
+        self.assertFalse(inspect.iscoroutinefunction(Cls.sync))
+        self.assertTrue(inspect.iscoroutinefunction(Cls.coro))
+
 def setUpModule():
     py_warnings.onceregistry.clear()
     c_warnings.onceregistry.clear()
index 20a39d54bf7e6a553e0865b24e3944f5e58e26a1..430e4748b973da0cb9b37ce14e67755dd529d2bd 100644 (file)
@@ -629,12 +629,16 @@ class deprecated:
             return arg
         elif callable(arg):
             import functools
+            import inspect
 
             @functools.wraps(arg)
             def wrapper(*args, **kwargs):
                 warn(msg, category=category, stacklevel=stacklevel + 1)
                 return arg(*args, **kwargs)
 
+            if inspect.iscoroutinefunction(arg):
+                wrapper = inspect.markcoroutinefunction(wrapper)
+
             arg.__deprecated__ = wrapper.__deprecated__ = msg
             return wrapper
         else:
diff --git a/Misc/NEWS.d/next/Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst b/Misc/NEWS.d/next/Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst
new file mode 100644 (file)
index 0000000..9c173d8
--- /dev/null
@@ -0,0 +1,3 @@
+:func:`@warnings.deprecated <warnings.deprecated>` now copies the
+coroutine status of functions and methods so that
+:func:`inspect.iscoroutinefunction` returns the correct result.