Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-108901: Deprecate inspect.getargs, slate it for removal in 3.15 #112279

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

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)
Comment on lines +1508 to +1511
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should follow our own advice and use inspect.signature(types.FunctionType(frame.f_code, {})) here? :)

Copy link
Member Author

@sobolevn sobolevn Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Right now getargs does some ugly things:

return Arguments(args + kwonlyargs, varargs, varkw)
It combines pos_or_kw_args together with kw_only_args, but ignores pos_only_args.

I think that we can just keep it as-is and then remove all of them together in 3.15 (because getargvalues will also be deprecated and removed at the same time, PR is just not ready yet).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see. Maybe we should do the getargvalues() deprecation first, though, in that case? It makes me a little uncomfortable adding this deprecation warning now, if we're not able to make the mandated change in our own code yet.

(I'm fine suppressing DeprecationWarnings in a function that is itself deprecated, but getargvalues() isn't, yet. And I know you plan to work on deprecating it immediately after this PR, but if I had a pound for every time somebody promised me they'd work on something the next day, and then discovered it was harder than they expected, I'd be a rich man :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can open a PR about getargvalues() first 👍

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, {}))``.
Loading