Skip to content

Commit 7b04496

Browse files
gh-125542: Deprecate prefix_chars in ArgumentParser.add_argument_group() (GH-125563)
1 parent 624be86 commit 7b04496

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

Doc/deprecations/pending-removal-in-future.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ Pending removal in future versions
44
The following APIs will be removed in the future,
55
although there is currently no date scheduled for their removal.
66

7-
* :mod:`argparse`: Nesting argument groups and nesting mutually exclusive
8-
groups are deprecated.
7+
* :mod:`argparse`:
8+
9+
* Nesting argument groups and nesting mutually exclusive
10+
groups are deprecated.
11+
* Passing the undocumented keyword argument *prefix_chars* to
12+
:meth:`~argparse.ArgumentParser.add_argument_group` is now
13+
deprecated.
914

1015
* :mod:`array`'s ``'u'`` format code (:gh:`57281`)
1116

Doc/library/argparse.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,10 @@ Argument groups
18941894
The function exists on the API by accident through inheritance and
18951895
will be removed in the future.
18961896

1897+
.. deprecated:: 3.14
1898+
Passing prefix_chars_ to :meth:`add_argument_group`
1899+
is now deprecated.
1900+
18971901

18981902
Mutual exclusion
18991903
^^^^^^^^^^^^^^^^

Doc/whatsnew/3.14.rst

+6
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@ asyncio
428428
Deprecated
429429
==========
430430

431+
* :mod:`argparse`:
432+
Passing the undocumented keyword argument *prefix_chars* to
433+
:meth:`~argparse.ArgumentParser.add_argument_group` is now
434+
deprecated.
435+
(Contributed by Savannah Ostrowski in :gh:`125563`.)
436+
431437
* :mod:`asyncio`:
432438
:func:`!asyncio.iscoroutinefunction` is deprecated
433439
and will be removed in Python 3.16,

Lib/argparse.py

+8
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,14 @@ def _check_help(self, action):
16621662
class _ArgumentGroup(_ActionsContainer):
16631663

16641664
def __init__(self, container, title=None, description=None, **kwargs):
1665+
if 'prefix_chars' in kwargs:
1666+
import warnings
1667+
depr_msg = (
1668+
"The use of the undocumented 'prefix_chars' parameter in "
1669+
"ArgumentParser.add_argument_group() is deprecated."
1670+
)
1671+
warnings.warn(depr_msg, DeprecationWarning, stacklevel=3)
1672+
16651673
# add any missing keyword arguments by checking the container
16661674
update = kwargs.setdefault
16671675
update('conflict_handler', container.conflict_handler)

Lib/test/test_argparse.py

+25
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,31 @@ def test_interleaved_groups(self):
28932893
result = parser.parse_args('1 2 3 4'.split())
28942894
self.assertEqual(expected, result)
28952895

2896+
class TestGroupConstructor(TestCase):
2897+
def test_group_prefix_chars(self):
2898+
parser = ErrorRaisingArgumentParser()
2899+
msg = (
2900+
"The use of the undocumented 'prefix_chars' parameter in "
2901+
"ArgumentParser.add_argument_group() is deprecated."
2902+
)
2903+
with self.assertWarns(DeprecationWarning) as cm:
2904+
parser.add_argument_group(prefix_chars='-+')
2905+
self.assertEqual(msg, str(cm.warning))
2906+
self.assertEqual(cm.filename, __file__)
2907+
2908+
def test_group_prefix_chars_default(self):
2909+
# "default" isn't quite the right word here, but it's the same as
2910+
# the parser's default prefix so it's a good test
2911+
parser = ErrorRaisingArgumentParser()
2912+
msg = (
2913+
"The use of the undocumented 'prefix_chars' parameter in "
2914+
"ArgumentParser.add_argument_group() is deprecated."
2915+
)
2916+
with self.assertWarns(DeprecationWarning) as cm:
2917+
parser.add_argument_group(prefix_chars='-')
2918+
self.assertEqual(msg, str(cm.warning))
2919+
self.assertEqual(cm.filename, __file__)
2920+
28962921
# ===================
28972922
# Parent parser tests
28982923
# ===================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate passing keyword-only *prefix_chars* argument to
2+
:meth:`argparse.ArgumentParser.add_argument_group`.

0 commit comments

Comments
 (0)