Skip to content

gh-108901: Deprecate inspect.getcallargs and slate it for removal #112236

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 2 additions & 15 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1086,24 +1086,11 @@ Classes and functions
``**`` arguments, if any) to their values from *args* and *kwds*. In case of
invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise
an exception because of incompatible signature, an exception of the same type
and the same or similar message is raised. For example::

>>> from inspect import getcallargs
>>> def f(a, b=1, *pos, **named):
... pass
...
>>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
True
>>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
True
>>> getcallargs(f)
Traceback (most recent call last):
...
TypeError: f() missing 1 required positional argument: 'a'
and the same or similar message is raised.

.. versionadded:: 3.2

.. deprecated:: 3.5
.. deprecated-removed:: 3.5 3.15
Use :meth:`Signature.bind` and :meth:`Signature.bind_partial` instead.


Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ 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`.)

* :func:`inspect.getcallargs` has been deprecated since Python 3.5.
Issue a runtime warning and slate it for removal in Python 3.15.
Use :meth:`inspect.Signature.bind`
or :meth:`inspect.Signature.bind_partial` instead.
(Contributed by Nikita Sobolev in :gh:`108901`.)

Pending Removal in Python 3.16
------------------------------

Expand Down
7 changes: 7 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,13 @@ def getcallargs(func, /, *positional, **named):
A dict is returned, with keys the function argument names (including the
names of the * and ** arguments, if any), and values the respective bound
values from 'positional' and 'named'."""
import warnings
warnings._deprecated(
"getcallargs",
'{name!r} is deprecated since Python 3.5 and slated for removal in Python {remove}; '
'use `inspect.Singature.bind` instead',
remove=(3, 15),
)
spec = getfullargspec(func)
args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
f_name = func.__name__
Expand Down
23 changes: 17 additions & 6 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,12 +1810,20 @@ def test_builtins_as_module(self):


class TestGetcallargsFunctions(unittest.TestCase):
def assertDeprecated(self):
import re
return self.assertWarnsRegex(
DeprecationWarning,
re.escape("'getcallargs' is deprecated since Python 3.5 "
"and slated for removal in Python 3.15"),
)

def assertEqualCallArgs(self, func, call_params_string, locs=None):
locs = dict(locs or {}, func=func)
r1 = eval('func(%s)' % call_params_string, None, locs)
r2 = eval('inspect.getcallargs(func, %s)' % call_params_string, None,
locs)
with self.assertDeprecated():
r2 = eval('inspect.getcallargs(func, %s)' % call_params_string, None,
locs)
self.assertEqual(r1, r2)

def assertEqualException(self, func, call_param_string, locs=None):
Expand All @@ -1827,8 +1835,9 @@ def assertEqualException(self, func, call_param_string, locs=None):
else:
self.fail('Exception not raised')
try:
eval('inspect.getcallargs(func, %s)' % call_param_string, None,
locs)
with self.assertDeprecated():
eval('inspect.getcallargs(func, %s)' % call_param_string, None,
locs)
except Exception as e:
ex2 = e
else:
Expand Down Expand Up @@ -1987,14 +1996,16 @@ def test_errors(self):
def f5(*, a): pass
with self.assertRaisesRegex(TypeError,
'missing 1 required keyword-only'):
inspect.getcallargs(f5)
with self.assertDeprecated():
inspect.getcallargs(f5)


# issue20817:
def f6(a, b, c):
pass
with self.assertRaisesRegex(TypeError, "'a', 'b' and 'c'"):
inspect.getcallargs(f6)
with self.assertDeprecated():
inspect.getcallargs(f6)

# bpo-33197
with self.assertRaisesRegex(ValueError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a runtime ``DeprecationWarning`` to :func:`inspect.getcallargs` and
slate it for removal in Python 3.15. It was soft-deprecated in Python 3.5.
Use :meth:`inspect.Signature.bind` instead.