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-92248: Deprecate type, choices, metavar parameters of argparse.BooleanOptionalAction #103678

Merged
merged 6 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,12 @@ Pending Removal in Python 3.14
* The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
and will be removed in 3.14.

* *type*, *chocies*, and *metavar* parameters
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
of :class:`!argparse.BooleanOptionalAction` are deprecated
and will be removed in 3.14
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
(Contributed by Nikita Sobolev in :gh:`92248`.)


Pending Removal in Future Versions
----------------------------------

Expand Down
27 changes: 24 additions & 3 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,16 +882,19 @@ def __call__(self, parser, namespace, values, option_string=None):
raise NotImplementedError(_('.__call__() not defined'))


# FIXME: remove together with `BooleanOptionalAction` deprecated arguments.
_deprecated_default = object()

class BooleanOptionalAction(Action):
def __init__(self,
option_strings,
dest,
default=None,
type=None,
choices=None,
type=_deprecated_default,
choices=_deprecated_default,
required=False,
help=None,
metavar=None):
metavar=_deprecated_default):

_option_strings = []
for option_string in option_strings:
Expand All @@ -901,6 +904,24 @@ def __init__(self,
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)

# We need `_deprecated` special value to ban explicit arguments that
# match default value. Like:
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
for field_name in ('type', 'choices', 'metavar'):
if locals()[field_name] is not _deprecated_default:
warnings._deprecated(
field_name,
"{name!r} is deprecated as of Python 3.12 and will be "
"removed in Python {remove}.",
remove=(3, 14))

if type is _deprecated_default:
type = None
if choices is _deprecated_default:
choices = None
if metavar is _deprecated_default:
metavar = None
hugovk marked this conversation as resolved.
Show resolved Hide resolved

super().__init__(
option_strings=_option_strings,
dest=dest,
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,49 @@ def test_const(self):

self.assertIn("got an unexpected keyword argument 'const'", str(cm.exception))

def test_deprecated_init_kw(self):
# See gh-92248
parser = argparse.ArgumentParser()

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-a',
action=argparse.BooleanOptionalAction,
type=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-b',
action=argparse.BooleanOptionalAction,
type=bool,
)

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-c',
action=argparse.BooleanOptionalAction,
metavar=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-d',
action=argparse.BooleanOptionalAction,
metavar='d',
)

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-e',
action=argparse.BooleanOptionalAction,
choices=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-f',
action=argparse.BooleanOptionalAction,
choices=(),
)

class TestBooleanOptionalActionRequired(ParserTestCase):
"""Tests BooleanOptionalAction required"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate ``type``, ``chocies``, and ``metavar`` parameters of
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
``argparse.BooleanOptionalAction``.