From 18ae53938ec24ea2b734094e2bc2ff683cf6bf78 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 20 Nov 2023 12:33:39 +0300 Subject: [PATCH 1/6] gh-108901: Deprecate `inspect.getargs`, slate it for removal in 3.15 --- Doc/whatsnew/3.13.rst | 4 ++++ Lib/inspect.py | 17 +++++++++++++++-- Lib/test/test_inspect/test_inspect.py | 11 +++++++++++ ...23-11-20-12-31-14.gh-issue-108901.abVgVe.rst | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 4d05bce34ef847..318ef56f45efdd 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,6 +581,10 @@ Pending Removal in Python 3.15 All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.) +* ``inspect.getargs`` is deprecated in 3.13 and slated for removal in 3.15, + instead use ``inspect.signature(types.FunctionType(co, {}))``. + (Contributed by Nikita Sobolev in :gh:`108901`.) + Pending Removal in Python 3.16 ------------------------------ diff --git a/Lib/inspect.py b/Lib/inspect.py index aaa22bef896602..def14a6fca0bde 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1363,7 +1363,17 @@ def getargs(co): Three things are returned: (args, varargs, varkw), where 'args' is the list of argument names. Keyword-only arguments are appended. 'varargs' and 'varkw' are the names of the * and ** - arguments or None.""" + arguments or None. + + Deprecated. Use ``inspect.signature(types.FunctionType(co, {}))`` instead. + """ + import warnings + warnings._deprecated( + "getargs", + "{name!r} is deprecated and slated for removal in Python {remove}, " + "use `inspect.signature(types.FunctionType(co, {{}}))` instead", + remove=(3, 15), + ) if not iscode(co): raise TypeError('{!r} is not a code object'.format(co)) @@ -1489,7 +1499,10 @@ def getargvalues(frame): 'args' is a list of the argument names. 'varargs' and 'varkw' are the names of the * and ** arguments or None. 'locals' is the locals dictionary of the given frame.""" - args, varargs, varkw = getargs(frame.f_code) + import warnings + with warnings.catch_warnings(): + warnings.simplefilter('ignore', category=DeprecationWarning) + args, varargs, varkw = getargs(frame.f_code) return ArgInfo(args, varargs, varkw, frame.f_locals) def formatannotation(annotation, base_module=None): diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index becbb0498bbb3f..6a78334aa59540 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -5022,6 +5022,17 @@ def f(): self.assertIn(expected, output) +class TestGetArgs(unittest.TestCase): + def test_getargs_deprecated(self): + import re + + def func(a, b): ... + + with self.assertWarnsRegex( + DeprecationWarning, + re.escape("'getargs' is deprecated and slated for removal in Python 3.15"), + ): + inspect.getargs(func.__code__) if __name__ == "__main__": diff --git a/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst new file mode 100644 index 00000000000000..89640f4841fe5e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst @@ -0,0 +1,2 @@ +Deprecate undocumented ``inspect.getargs`` function. Instead use +``inspect.signature(types.FunctionType(co, {}))``. From fd81c14d8b349ba812e956af16e7976d268d7fae Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 22 Nov 2023 15:52:03 +0300 Subject: [PATCH 2/6] Address review --- Doc/whatsnew/3.13.rst | 7 +++++-- Lib/inspect.py | 8 ++++++-- .../2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst | 8 ++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 318ef56f45efdd..c60e6f7b2acd3c 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,8 +581,11 @@ Pending Removal in Python 3.15 All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.) -* ``inspect.getargs`` is deprecated in 3.13 and slated for removal in 3.15, - instead use ``inspect.signature(types.FunctionType(co, {}))``. +* ``inspect.getargs`` is deprecated in 3.13 and slated for removal in 3.15. + :func:`inspect.signature` is the most accurate way to obtain the signature + of a callable. If you only have access to a code object, however, + ``inspect.signature(types.FunctionType(co, {}))`` can be used as a + direct replacement for ``inspect.getargs()``. (Contributed by Nikita Sobolev in :gh:`108901`.) Pending Removal in Python 3.16 diff --git a/Lib/inspect.py b/Lib/inspect.py index def14a6fca0bde..d42ff22e5f67f8 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1365,13 +1365,17 @@ def getargs(co): appended. 'varargs' and 'varkw' are the names of the * and ** arguments or None. - Deprecated. Use ``inspect.signature(types.FunctionType(co, {}))`` instead. + Deprecated in 3.13 and slated for removal in 3.15. + ``inspect.signature`` is the most accurate way to obtain the signature + of a callable. If you only have access to a code object, however, + ``inspect.signature(types.FunctionType(co, {}))`` can be used as a + direct replacement for ``inspect.getargs()``. """ import warnings warnings._deprecated( "getargs", "{name!r} is deprecated and slated for removal in Python {remove}, " - "use `inspect.signature(types.FunctionType(co, {{}}))` instead", + "use `inspect.signature` instead", remove=(3, 15), ) if not iscode(co): diff --git a/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst index 89640f4841fe5e..ec6e28b3413b03 100644 --- a/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst +++ b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst @@ -1,2 +1,6 @@ -Deprecate undocumented ``inspect.getargs`` function. Instead use -``inspect.signature(types.FunctionType(co, {}))``. +Undocumented ``inspect.getargs`` is deprecated in 3.13 +and slated for removal in 3.15. +:func:`inspect.signature` is the most accurate way to obtain the signature +of a callable. If you only have access to a code object, however, +``inspect.signature(types.FunctionType(co, {}))`` can be used as a +direct replacement for ``inspect.getargs()``. From 6087111599a54080b1e60c56048287854bb896d6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 22 Nov 2023 16:00:25 +0300 Subject: [PATCH 3/6] Address review --- Lib/inspect.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index d42ff22e5f67f8..87d7034adda7c3 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1374,8 +1374,10 @@ def getargs(co): import warnings warnings._deprecated( "getargs", - "{name!r} is deprecated and slated for removal in Python {remove}, " - "use `inspect.signature` instead", + ( + "{name!r} is deprecated and slated for removal in Python {remove}, " + "use `inspect.signature` instead." + ), remove=(3, 15), ) if not iscode(co): From 4eea14cc8ae175677fbdf1fcafd80c5e47419f96 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 22 Nov 2023 16:10:48 +0300 Subject: [PATCH 4/6] Address review --- Lib/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 87d7034adda7c3..876956598ef1bf 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1375,7 +1375,7 @@ def getargs(co): warnings._deprecated( "getargs", ( - "{name!r} is deprecated and slated for removal in Python {remove}, " + "{name!r} is deprecated and slated for removal in Python {remove}; " "use `inspect.signature` instead." ), remove=(3, 15), From d8cd4f228f32d2d196407776e7bb80e472d0ed2c Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 22 Nov 2023 16:28:34 +0300 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Alex Waygood --- Doc/whatsnew/3.13.rst | 10 +++++----- .../2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index c60e6f7b2acd3c..e703eb8351b314 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,11 +581,11 @@ Pending Removal in Python 3.15 All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.) -* ``inspect.getargs`` is deprecated in 3.13 and slated for removal in 3.15. - :func:`inspect.signature` is the most accurate way to obtain the signature - of a callable. If you only have access to a code object, however, - ``inspect.signature(types.FunctionType(co, {}))`` can be used as a - direct replacement for ``inspect.getargs()``. +* The undocumented function ``inspect.getargs`` is deprecated in Python 3.13 + and slated for removal in Python 3.15. :func:`inspect.signature` is the most + accurate way to obtain the signature of a callable. If you only have access + to a code object, however, ``inspect.signature(types.FunctionType(co, {}))`` + can be used as a direct replacement for ``inspect.getargs()``. (Contributed by Nikita Sobolev in :gh:`108901`.) Pending Removal in Python 3.16 diff --git a/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst index ec6e28b3413b03..032720a8fd7de3 100644 --- a/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst +++ b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst @@ -1,5 +1,5 @@ -Undocumented ``inspect.getargs`` is deprecated in 3.13 -and slated for removal in 3.15. +The undocumented function ``inspect.getargs`` is deprecated in +Python 3.13 and slated for removal in Python 3.15. :func:`inspect.signature` is the most accurate way to obtain the signature of a callable. If you only have access to a code object, however, ``inspect.signature(types.FunctionType(co, {}))`` can be used as a From 56dbc0f9bb41c488a9172f07c89d409d9b422815 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 22 Nov 2023 13:31:40 +0000 Subject: [PATCH 6/6] one more nit --- Lib/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 876956598ef1bf..ff50b1bf496089 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1365,7 +1365,7 @@ def getargs(co): appended. 'varargs' and 'varkw' are the names of the * and ** arguments or None. - Deprecated in 3.13 and slated for removal in 3.15. + Deprecated in Python 3.13; slated for removal in Python 3.15. ``inspect.signature`` is the most accurate way to obtain the signature of a callable. If you only have access to a code object, however, ``inspect.signature(types.FunctionType(co, {}))`` can be used as a