Skip to content

Commit

Permalink
pythongh-118805: Remove type, choices, metavar params of `BooleanOpti…
Browse files Browse the repository at this point in the history
…onalAction` (python#118806)

Co-authored-by: Alex Waygood <[email protected]>
  • Loading branch information
sobolevn and AlexWaygood authored May 9, 2024
1 parent c68acb1 commit da090f1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 71 deletions.
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ Deprecated
Removed
=======

argparse
--------

* The *type*, *choices*, and *metavar* parameters
of :class:`!argparse.BooleanOptionalAction` are removed.
They were deprecated since 3.12.

email
-----

Expand Down
28 changes: 0 additions & 28 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,19 +831,13 @@ 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=_deprecated_default,
choices=_deprecated_default,
required=False,
help=None,
metavar=_deprecated_default,
deprecated=False):

_option_strings = []
Expand All @@ -854,35 +848,13 @@ 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:
import warnings
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

super().__init__(
option_strings=_option_strings,
dest=dest,
nargs=0,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar,
deprecated=deprecated)


Expand Down
43 changes: 0 additions & 43 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,49 +765,6 @@ 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,3 @@
Remove *type*, *choices*, and *metavar* parameters of
:class:`!argparse.BooleanOptionalAction`.
They were deprecated since Python 3.12.

0 comments on commit da090f1

Please sign in to comment.