diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 4d05bce34ef847..e703eb8351b314 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,6 +581,13 @@ 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`.) +* 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/Lib/inspect.py b/Lib/inspect.py index aaa22bef896602..ff50b1bf496089 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1363,7 +1363,23 @@ 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 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 + direct replacement for ``inspect.getargs()``. + """ + import warnings + warnings._deprecated( + "getargs", + ( + "{name!r} is deprecated and slated for removal in Python {remove}; " + "use `inspect.signature` instead." + ), + remove=(3, 15), + ) if not iscode(co): raise TypeError('{!r} is not a code object'.format(co)) @@ -1489,7 +1505,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..032720a8fd7de3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-20-12-31-14.gh-issue-108901.abVgVe.rst @@ -0,0 +1,6 @@ +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()``.