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-125542: Deprecate prefix_chars in ArgumentParser.add_argument_group #125563

Merged
merged 13 commits into from
Oct 17, 2024
4 changes: 4 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
Passing prefix_chars_ to :meth:`add_argument_group`
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
is now deprecated.


Mutual exclusion
^^^^^^^^^^^^^^^^
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@
Deprecated
==========

* :mod:`argparse`:

Check warning on line 423 in Doc/whatsnew/3.14.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: argparse.add_argument_group [ref.meth]
Passing *prefix_chars* to :meth:`argparse.add_argument_group`
is now deprecated.
(Contributed by Savannah Ostrowski in :gh:`125563`.)

* :mod:`asyncio`:
:func:`!asyncio.iscoroutinefunction` is deprecated
and will be removed in Python 3.16,
Expand Down
8 changes: 8 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,14 @@ 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."
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
)
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)
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,36 @@ 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='-+')
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved

def test_group_prefix_chars_default(self):
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
# "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."
)

with self.assertWarnsRegex(DeprecationWarning, msg):
parser.add_argument_group(prefix_chars='-')

def test_group_without_prefix_chars(self):
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
parser = ErrorRaisingArgumentParser()
group = parser.add_argument_group()
group.add_argument('--foo')
self.assertEqual(parser.parse_args(['--foo', 'bar']), NS(foo='bar'))

# ===================
# Parent parser tests
# ===================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate passing prefix_chars_ parameter to :meth:`ArgumentParser.add_argument_group`.
savannahostrowski marked this conversation as resolved.
Show resolved Hide resolved
Loading