Skip to content

Commit 375c9f6

Browse files
srittaupicnixz
andauthored
gh-122088: Copy the coroutine status of the underlying callable in @warnings.deprecated (#122086)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 2a5d1eb commit 375c9f6

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Diff for: Lib/test/test_warnings/__init__.py

+24
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
@@ -1684,6 +1685,29 @@ def d(): pass
16841685
isinstance(cell.cell_contents, deprecated) for cell in d.__closure__
16851686
))
16861687

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

Diff for: Lib/warnings.py

+4
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,16 @@ def __init_subclass__(*args, **kwargs):
628628
return arg
629629
elif callable(arg):
630630
import functools
631+
import inspect
631632

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

638+
if inspect.iscoroutinefunction(arg):
639+
wrapper = inspect.markcoroutinefunction(wrapper)
640+
637641
arg.__deprecated__ = wrapper.__deprecated__ = msg
638642
return wrapper
639643
else:
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)