Skip to content

Commit

Permalink
pythongh-108901: Deprecate inspect.getargs, slate it for removal in…
Browse files Browse the repository at this point in the history
… 3.15
  • Loading branch information
sobolevn committed Nov 20, 2023
1 parent ce1096f commit 18ae539
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------

Expand Down
17 changes: 15 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate undocumented ``inspect.getargs`` function. Instead use
``inspect.signature(types.FunctionType(co, {}))``.

0 comments on commit 18ae539

Please sign in to comment.