From bd26604e143f846669402243f5fd82e6805442ee Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 21:46:27 -0700 Subject: [PATCH 01/12] Deprecate prefix_chars --- Doc/library/argparse.rst | 4 ++++ Doc/whatsnew/3.14.rst | 5 +++++ Lib/argparse.py | 9 +++++++++ Lib/test/test_argparse.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 51ab8e29ff96d5..d9e8a0023bdc8c 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1868,6 +1868,10 @@ Argument groups The function exists on the API by accident through inheritance and will be removed in the future. + .. versionchanged:: 3.14 + Passing the prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group` + is now deprecated. + Mutual exclusion ^^^^^^^^^^^^^^^^ diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index b106578fe9e8b0..48fce80b2374b7 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -420,6 +420,11 @@ asyncio Deprecated ========== +* :mod:`argparse`: + Passing the prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group` + is now deprecated. + (Contributed by Savannah Ostrowski in :gh:`xxxx`.) + * :mod:`asyncio`: :func:`!asyncio.iscoroutinefunction` is deprecated and will be removed in Python 3.16, diff --git a/Lib/argparse.py b/Lib/argparse.py index fa9f5211257e96..96ba8421acb0a2 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1662,6 +1662,15 @@ def _check_help(self, action): class _ArgumentGroup(_ActionsContainer): def __init__(self, container, title=None, description=None, **kwargs): + + if 'prefix_chars' in kwargs: + import warnings + depr_msg = ( + "The use of the undocumented 'prefix_chars' parameter in " + "ArgumentParser.add_argument_group is deprecated." + ) + warnings.warn(depr_msg, DeprecationWarning, stacklevel=3) + # add any missing keyword arguments by checking the container update = kwargs.setdefault update('conflict_handler', container.conflict_handler) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 78692fd3474782..3c2fd0279e94af 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2816,6 +2816,37 @@ def test_interleaved_groups(self): result = parser.parse_args('1 2 3 4'.split()) self.assertEqual(expected, result) +class TestGroupConstructor(TestCase): + def test_group_prefix_chars(self): + parser = ErrorRaisingArgumentParser() + + msg = ( + "The use of the undocumented 'prefix_chars' parameter in " + "ArgumentParser.add_argument_group is deprecated." + ) + with self.assertWarnsRegex(DeprecationWarning, msg): + parser.add_argument_group(prefix_chars='-+') + + def test_group_prefix_chars_default(self): + # "default" isn't quite the right word here, but it's the same as + # the parser's default prefix so it's a good test + parser = ErrorRaisingArgumentParser() + + msg = ( + "The use of the undocumented 'prefix_chars' parameter in " + "ArgumentParser.add_argument_group is deprecated." + ) + + # The parser uses a default of '-' if prefix_chars is not set + with self.assertWarnsRegex(DeprecationWarning, msg): + parser.add_argument_group(prefix_chars='-') + + def test_group_without_prefix_chars(self): + parser = ErrorRaisingArgumentParser() + group = parser.add_argument_group() + group.add_argument('--foo') + self.assertEqual(parser.parse_args(['--foo', 'bar']), NS(foo='bar')) + # =================== # Parent parser tests # =================== From 0763efbf6f7358ea3cb9a2128bb4360deb6f0278 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 21:47:34 -0700 Subject: [PATCH 02/12] Remove extraneous comment --- Lib/test/test_argparse.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 3c2fd0279e94af..39702a6e0330e9 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2837,7 +2837,6 @@ def test_group_prefix_chars_default(self): "ArgumentParser.add_argument_group is deprecated." ) - # The parser uses a default of '-' if prefix_chars is not set with self.assertWarnsRegex(DeprecationWarning, msg): parser.add_argument_group(prefix_chars='-') From fd1cd64b11ecf176d1908de4a8388d313001dfb9 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 21:49:45 -0700 Subject: [PATCH 03/12] Add GH number to whatsnew --- Doc/whatsnew/3.14.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 48fce80b2374b7..6f721315ea3947 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -423,7 +423,7 @@ Deprecated * :mod:`argparse`: Passing the prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group` is now deprecated. - (Contributed by Savannah Ostrowski in :gh:`xxxx`.) + (Contributed by Savannah Ostrowski in :gh:`125563`.) * :mod:`asyncio`: :func:`!asyncio.iscoroutinefunction` is deprecated From faf7699799686d583938f16d663e20b929f82907 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 04:50:55 +0000 Subject: [PATCH 04/12] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst new file mode 100644 index 00000000000000..202c15b014e9b7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst @@ -0,0 +1 @@ +Deprecate passing prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group`. From f105c3e44d34a731a1d0e0949c895475ec13c50e Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 21:51:46 -0700 Subject: [PATCH 05/12] Remove newline --- Lib/argparse.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 96ba8421acb0a2..4c227077512b7b 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1662,7 +1662,6 @@ def _check_help(self, action): class _ArgumentGroup(_ActionsContainer): def __init__(self, container, title=None, description=None, **kwargs): - if 'prefix_chars' in kwargs: import warnings depr_msg = ( From b5f23552c5af6af6b55892b90b214655d4fce16f Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 22:02:39 -0700 Subject: [PATCH 06/12] Appease linter --- Doc/library/argparse.rst | 4 ++-- Doc/whatsnew/3.14.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index d9e8a0023bdc8c..ea6e8973c78121 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1869,8 +1869,8 @@ Argument groups will be removed in the future. .. versionchanged:: 3.14 - Passing the prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group` - is now deprecated. + Passing the prefix_chars_ parameter to :meth:`add_argument_group` + is now deprecated. Mutual exclusion diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 6f721315ea3947..c1f1e7651b3950 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -421,7 +421,7 @@ Deprecated ========== * :mod:`argparse`: - Passing the prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group` + Passing the prefix_chars_ parameter to :meth:`add_argument_group` is now deprecated. (Contributed by Savannah Ostrowski in :gh:`125563`.) From 7c0c570fea96a0493a0a1cb2fbe6a5ef8ad6bd32 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 22:20:01 -0700 Subject: [PATCH 07/12] Fix reference --- Doc/whatsnew/3.14.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index c1f1e7651b3950..1111a7fc405ec0 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -421,7 +421,7 @@ Deprecated ========== * :mod:`argparse`: - Passing the prefix_chars_ parameter to :meth:`add_argument_group` + Passing *prefix_chars* to :meth:`argparse.add_argument_group` is now deprecated. (Contributed by Savannah Ostrowski in :gh:`125563`.) From 86d91eff3a627cf4bf3cebce3cc9b0c514f8de8c Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 22:23:13 -0700 Subject: [PATCH 08/12] Update deprecation in docs for consistency --- Doc/library/argparse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index ea6e8973c78121..9862790b4cb8fe 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1869,7 +1869,7 @@ Argument groups will be removed in the future. .. versionchanged:: 3.14 - Passing the prefix_chars_ parameter to :meth:`add_argument_group` + Passing prefix_chars_ to :meth:`add_argument_group` is now deprecated. From 5897e0ac5e9798be122585ab4658335554ecbca1 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 15 Oct 2024 22:30:52 -0700 Subject: [PATCH 09/12] Use func --- Doc/whatsnew/3.14.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 1111a7fc405ec0..c74e5ead5cdb6c 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -421,7 +421,7 @@ Deprecated ========== * :mod:`argparse`: - Passing *prefix_chars* to :meth:`argparse.add_argument_group` + Passing *prefix_chars* to :func:`argparse.add_argument_group` is now deprecated. (Contributed by Savannah Ostrowski in :gh:`125563`.) From 3d0ddf3d2e50b73e2cb7065f6bdfa90f46340f73 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Wed, 16 Oct 2024 19:45:24 -0700 Subject: [PATCH 10/12] Address PR comments --- .../pending-removal-in-future.rst | 9 ++++++-- Doc/library/argparse.rst | 2 +- Doc/whatsnew/3.14.rst | 5 +++-- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 21 +++++++------------ ...-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst | 3 ++- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Doc/deprecations/pending-removal-in-future.rst b/Doc/deprecations/pending-removal-in-future.rst index f916797c07a068..d77fc86eab0ed6 100644 --- a/Doc/deprecations/pending-removal-in-future.rst +++ b/Doc/deprecations/pending-removal-in-future.rst @@ -4,8 +4,13 @@ Pending removal in future versions The following APIs will be removed in the future, although there is currently no date scheduled for their removal. -* :mod:`argparse`: Nesting argument groups and nesting mutually exclusive - groups are deprecated. +* :mod:`argparse`: + + * Nesting argument groups and nesting mutually exclusive + groups are deprecated. + * Passing the undocumented keyword argument *prefix_chars* to + :meth:`~argparse.ArgumentParser.add_argument_group` is now + deprecated. * :mod:`array`'s ``'u'`` format code (:gh:`57281`) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 9862790b4cb8fe..a72c391c0ddf4e 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1868,7 +1868,7 @@ Argument groups The function exists on the API by accident through inheritance and will be removed in the future. - .. versionchanged:: 3.14 + .. deprecated:: 3.14 Passing prefix_chars_ to :meth:`add_argument_group` is now deprecated. diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index c74e5ead5cdb6c..3bbbdc778328b5 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -421,8 +421,9 @@ Deprecated ========== * :mod:`argparse`: - Passing *prefix_chars* to :func:`argparse.add_argument_group` - is now deprecated. + Passing the undocumented keyword argument *prefix_chars* to + :meth:`~argparse.ArgumentParser.add_argument_group` is now + deprecated. (Contributed by Savannah Ostrowski in :gh:`125563`.) * :mod:`asyncio`: diff --git a/Lib/argparse.py b/Lib/argparse.py index 4c227077512b7b..10c7d0f7e63295 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1666,7 +1666,7 @@ def __init__(self, container, title=None, description=None, **kwargs): import warnings depr_msg = ( "The use of the undocumented 'prefix_chars' parameter in " - "ArgumentParser.add_argument_group is deprecated." + "ArgumentParser.add_argument_group() is deprecated." ) warnings.warn(depr_msg, DeprecationWarning, stacklevel=3) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 39702a6e0330e9..d39bd8dba628ea 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2819,32 +2819,27 @@ def test_interleaved_groups(self): class TestGroupConstructor(TestCase): def test_group_prefix_chars(self): parser = ErrorRaisingArgumentParser() - msg = ( "The use of the undocumented 'prefix_chars' parameter in " - "ArgumentParser.add_argument_group is deprecated." + "ArgumentParser.add_argument_group() is deprecated." ) - with self.assertWarnsRegex(DeprecationWarning, msg): + with self.assertWarns(DeprecationWarning) as cm: parser.add_argument_group(prefix_chars='-+') + self.assertEqual(msg, str(cm.warning)) + self.assertEqual(cm.filename, __file__) def test_group_prefix_chars_default(self): # "default" isn't quite the right word here, but it's the same as # the parser's default prefix so it's a good test parser = ErrorRaisingArgumentParser() - msg = ( "The use of the undocumented 'prefix_chars' parameter in " - "ArgumentParser.add_argument_group is deprecated." + "ArgumentParser.add_argument_group() is deprecated." ) - - with self.assertWarnsRegex(DeprecationWarning, msg): + with self.assertWarns(DeprecationWarning) as cm: parser.add_argument_group(prefix_chars='-') - - def test_group_without_prefix_chars(self): - parser = ErrorRaisingArgumentParser() - group = parser.add_argument_group() - group.add_argument('--foo') - self.assertEqual(parser.parse_args(['--foo', 'bar']), NS(foo='bar')) + self.assertEqual(msg, str(cm.warning)) + self.assertEqual(cm.filename, __file__) # =================== # Parent parser tests diff --git a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst index 202c15b014e9b7..1126b286df1133 100644 --- a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst +++ b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst @@ -1 +1,2 @@ -Deprecate passing prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group`. +Deprecate passing keyword-only :ref:`prefix_chars` argument to +:meth:`argparse.ArgumentParser.add_argument_group`. From d3a2f4105b010b0c3e91f84fab207f286057c433 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Wed, 16 Oct 2024 19:53:30 -0700 Subject: [PATCH 11/12] Use double backtick syntax --- .../next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst index 1126b286df1133..2c2938511978b2 100644 --- a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst +++ b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst @@ -1,2 +1,2 @@ -Deprecate passing keyword-only :ref:`prefix_chars` argument to +Deprecate passing keyword-only ``prefix_chars`` argument to :meth:`argparse.ArgumentParser.add_argument_group`. From f43f6f22bfa42fb4da52baea2eff6f570fb7b847 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 17 Oct 2024 11:47:03 +0300 Subject: [PATCH 12/12] Update Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst --- .../next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst index 2c2938511978b2..777920cc54ff9b 100644 --- a/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst +++ b/Misc/NEWS.d/next/Library/2024-10-16-04-50-53.gh-issue-125542.vZJ-Ns.rst @@ -1,2 +1,2 @@ -Deprecate passing keyword-only ``prefix_chars`` argument to +Deprecate passing keyword-only *prefix_chars* argument to :meth:`argparse.ArgumentParser.add_argument_group`.