From cdcfe55da4cd9686bd0b3976a0b1710f31ec2be2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 18 Nov 2023 17:02:16 +0300 Subject: [PATCH 1/6] gh-108901: Deprecate `inspect.getcallargs` and slate it for removal --- Doc/library/inspect.rst | 17 ++------------ Doc/whatsnew/3.13.rst | 6 +++++ Lib/inspect.py | 7 ++++++ Lib/test/test_inspect/test_inspect.py | 23 ++++++++++++++----- ...-11-18-17-01-45.gh-issue-108901.T3p5ie.rst | 3 +++ 5 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index b463c0b6d0e402..9732c1034ac6e4 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -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. diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 4d05bce34ef847..6b4c9ea0330a08 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -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` was deprecated since 3.5, + now we issue a runtime warning and slate it for remove in 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 ------------------------------ diff --git a/Lib/inspect.py b/Lib/inspect.py index aaa22bef896602..c77ffc5ef79384 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 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__ diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index becbb0498bbb3f..d6dd218b776e7e 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -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 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): @@ -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: @@ -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, diff --git a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst new file mode 100644 index 00000000000000..396e0bc673230b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst @@ -0,0 +1,3 @@ +Add a runtime ``DeprecationWarning`` to :func:`inspect.getcallargs` and +slate it for remove in 3.15, since it was softly-deprecated since 3.5, use +:meth:`inspect.Signature.bind` instead. From 1387814d3e6016c25ded41c1810388e8f3cad687 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 18 Nov 2023 17:36:36 +0300 Subject: [PATCH 2/6] Update Lib/test/test_inspect/test_inspect.py Co-authored-by: Alex Waygood --- Lib/test/test_inspect/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index d6dd218b776e7e..47ff11e4fe6959 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -1815,7 +1815,7 @@ def assertDeprecated(self): return self.assertWarnsRegex( DeprecationWarning, re.escape("'getcallargs' is deprecated since 3.5 " - "and slated for removal in Python 3.15"), + "and slated for removal in Python 3.15"), ) def assertEqualCallArgs(self, func, call_params_string, locs=None): From ae95b87c3bca9de393c31827882630b58bed6bc4 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 18 Nov 2023 10:25:44 -0500 Subject: [PATCH 3/6] Update Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst --- .../next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst index 396e0bc673230b..818dd1d908c13c 100644 --- a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst +++ b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst @@ -1,3 +1,3 @@ Add a runtime ``DeprecationWarning`` to :func:`inspect.getcallargs` and -slate it for remove in 3.15, since it was softly-deprecated since 3.5, use +slate it for remove in 3.15. It was softly-deprecated in 3.5. Use :meth:`inspect.Signature.bind` instead. From 772e87aa5ecfef7399f813b0e480b0dc069bb5aa Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 18 Nov 2023 10:29:14 -0500 Subject: [PATCH 4/6] Update Doc/whatsnew/3.13.rst --- Doc/whatsnew/3.13.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 6b4c9ea0330a08..77995cd7677725 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,8 +581,8 @@ 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` was deprecated since 3.5, - now we issue a runtime warning and slate it for remove in 3.15. +* :func:`inspect.getcallargs` has been deprecated since 3.5. + Issue a runtime warning and slate it for remove in 3.15. Use :meth:`inspect.Signature.bind` or :meth:`inspect.Signature.bind_partial` instead. (Contributed by Nikita Sobolev in :gh:`108901`.) From bc0857f1318ba945083cb5788fbcf7b5d4ff5472 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 20 Nov 2023 10:32:25 +0300 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Itamar Oren --- Doc/whatsnew/3.13.rst | 2 +- .../next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 77995cd7677725..ca6287d7ced4dc 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -582,7 +582,7 @@ Pending Removal in Python 3.15 (Contributed by Nikita Sobolev in :gh:`102029`.) * :func:`inspect.getcallargs` has been deprecated since 3.5. - Issue a runtime warning and slate it for remove in 3.15. + Issue a runtime warning and slate it for removal in 3.15. Use :meth:`inspect.Signature.bind` or :meth:`inspect.Signature.bind_partial` instead. (Contributed by Nikita Sobolev in :gh:`108901`.) diff --git a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst index 818dd1d908c13c..aac2f304f57406 100644 --- a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst +++ b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst @@ -1,3 +1,3 @@ Add a runtime ``DeprecationWarning`` to :func:`inspect.getcallargs` and -slate it for remove in 3.15. It was softly-deprecated in 3.5. Use +slate it for removal in 3.15. It was softly-deprecated in 3.5. Use :meth:`inspect.Signature.bind` instead. From 3d98e87df60a260fad1a342eed3599ee7f475108 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 22 Nov 2023 16:29:37 +0300 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Alex Waygood --- Doc/library/inspect.rst | 2 +- Doc/whatsnew/3.13.rst | 4 ++-- Lib/inspect.py | 2 +- Lib/test/test_inspect/test_inspect.py | 2 +- .../Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 9732c1034ac6e4..691d6ae0589cc1 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -1090,7 +1090,7 @@ Classes and functions .. versionadded:: 3.2 - .. deprecated-removed:: 3.5, 3.15 + .. deprecated-removed:: 3.5 3.15 Use :meth:`Signature.bind` and :meth:`Signature.bind_partial` instead. diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index ca6287d7ced4dc..f56a8c469274fb 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -581,8 +581,8 @@ 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 3.5. - Issue a runtime warning and slate it for removal in 3.15. +* :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`.) diff --git a/Lib/inspect.py b/Lib/inspect.py index c77ffc5ef79384..f254c6e5428e5e 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1582,7 +1582,7 @@ def getcallargs(func, /, *positional, **named): import warnings warnings._deprecated( "getcallargs", - '{name!r} is deprecated since 3.5 and slated for removal in Python {remove}, ' + '{name!r} is deprecated since Python 3.5 and slated for removal in Python {remove}; ' 'use `inspect.Singature.bind` instead', remove=(3, 15), ) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 47ff11e4fe6959..b0e76c2621d386 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -1814,7 +1814,7 @@ def assertDeprecated(self): import re return self.assertWarnsRegex( DeprecationWarning, - re.escape("'getcallargs' is deprecated since 3.5 " + re.escape("'getcallargs' is deprecated since Python 3.5 " "and slated for removal in Python 3.15"), ) diff --git a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst index aac2f304f57406..8b763eb8397eca 100644 --- a/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst +++ b/Misc/NEWS.d/next/Library/2023-11-18-17-01-45.gh-issue-108901.T3p5ie.rst @@ -1,3 +1,3 @@ Add a runtime ``DeprecationWarning`` to :func:`inspect.getcallargs` and -slate it for removal in 3.15. It was softly-deprecated in 3.5. Use -:meth:`inspect.Signature.bind` instead. +slate it for removal in Python 3.15. It was soft-deprecated in Python 3.5. +Use :meth:`inspect.Signature.bind` instead.