From 94093f6a6ab48c2d3c02fde38d5a25a9518872af Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 21 Jul 2024 13:52:15 +0200 Subject: [PATCH 1/5] Copy the coroutine flag in @warnings.deprecated --- Lib/test/test_warnings/__init__.py | 25 +++++++++++++++++++++++++ Lib/warnings.py | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index f9b2b07fbd6576..4b4c2790d032b7 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -1684,6 +1684,31 @@ def d(): pass isinstance(cell.cell_contents, deprecated) for cell in d.__closure__ )) + def test_inspect(self): + import inspect + + @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() diff --git a/Lib/warnings.py b/Lib/warnings.py index d344ceaebb0378..e83cde37ab2d1a 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -628,12 +628,16 @@ def __init_subclass__(*args, **kwargs): 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: From afece5d1547a603212adbaee2c95ee09052cf036 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 21 Jul 2024 18:03:33 +0200 Subject: [PATCH 2/5] Add NEWS entry --- .../Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst 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 index 00000000000000..4801be16da74fb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst @@ -0,0 +1,3 @@ +``@warnings.deprecated`` now copies to coroutine flag of functions and +methods so that ``inspect.iscoroutinefunction()`` returns the correct +result. From 78b741e392e1dcb08a51e8c4d648906c8b64927f Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 21 Jul 2024 18:29:26 +0200 Subject: [PATCH 3/5] Link items in NEWS entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 4801be16da74fb..8abb9730028336 100644 --- 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 @@ -1,3 +1,4 @@ -``@warnings.deprecated`` now copies to coroutine flag of functions and -methods so that ``inspect.iscoroutinefunction()`` returns the correct +:func:`@warnings.deprecated ` now copies the +:data:`coroutine flag ` flag of functions and +methods so that :func:`inspect.iscoroutinefunction` returns the correct result. From 631e8626ac370fc686e9f479ed7f603dc4daf21d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 22 Jul 2024 00:02:03 +0200 Subject: [PATCH 4/5] Move import --- Lib/test/test_warnings/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 4b4c2790d032b7..7d04371c94abda 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -1,6 +1,7 @@ from contextlib import contextmanager import linecache import os +import inspect from io import StringIO import re import sys @@ -1685,8 +1686,6 @@ def d(): pass )) def test_inspect(self): - import inspect - @deprecated("depr") def sync(): pass From 03f43c1db0e5598dd3529e66adbacef27310c19f Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 23 Jul 2024 11:17:48 +0200 Subject: [PATCH 5/5] Correct NEWS entry --- .../Library/2024-07-21-18-03-30.gh-issue-122088.vi2bP-.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index 8abb9730028336..9c173d8c462feb 100644 --- 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 @@ -1,4 +1,3 @@ :func:`@warnings.deprecated ` now copies the -:data:`coroutine flag ` flag of functions and -methods so that :func:`inspect.iscoroutinefunction` returns the correct -result. +coroutine status of functions and methods so that +:func:`inspect.iscoroutinefunction` returns the correct result.