Skip to content

Commit f521ce1

Browse files
srittaupicnixz
authored andcommitted
pythongh-122088: Copy the coroutine status of the underlying callable in @warnings.deprecated (pythonGH-122086)
(cherry picked from commit 375c9f6) Co-authored-by: Sebastian Rittau <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 48154e7 commit f521ce1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22
import linecache
33
import os
4+
import inspect
45
from io import StringIO
56
import re
67
import sys
@@ -1682,6 +1683,29 @@ def d(): pass
16821683
isinstance(cell.cell_contents, deprecated) for cell in d.__closure__
16831684
))
16841685

1686+
def test_inspect(self):
1687+
@deprecated("depr")
1688+
def sync():
1689+
pass
1690+
1691+
@deprecated("depr")
1692+
async def coro():
1693+
pass
1694+
1695+
class Cls:
1696+
@deprecated("depr")
1697+
def sync(self):
1698+
pass
1699+
1700+
@deprecated("depr")
1701+
async def coro(self):
1702+
pass
1703+
1704+
self.assertFalse(inspect.iscoroutinefunction(sync))
1705+
self.assertTrue(inspect.iscoroutinefunction(coro))
1706+
self.assertFalse(inspect.iscoroutinefunction(Cls.sync))
1707+
self.assertTrue(inspect.iscoroutinefunction(Cls.coro))
1708+
16851709
def setUpModule():
16861710
py_warnings.onceregistry.clear()
16871711
c_warnings.onceregistry.clear()

Lib/warnings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,16 @@ def __init_subclass__(*args, **kwargs):
629629
return arg
630630
elif callable(arg):
631631
import functools
632+
import inspect
632633

633634
@functools.wraps(arg)
634635
def wrapper(*args, **kwargs):
635636
warn(msg, category=category, stacklevel=stacklevel + 1)
636637
return arg(*args, **kwargs)
637638

639+
if inspect.iscoroutinefunction(arg):
640+
wrapper = inspect.markcoroutinefunction(wrapper)
641+
638642
arg.__deprecated__ = wrapper.__deprecated__ = msg
639643
return wrapper
640644
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`@warnings.deprecated <warnings.deprecated>` now copies the
2+
coroutine status of functions and methods so that
3+
:func:`inspect.iscoroutinefunction` returns the correct result.

0 commit comments

Comments
 (0)