Skip to content

Commit 27a7d5e

Browse files
sobolevnEclips4hugovkarhadthedev
authored
gh-92248: Deprecate type, choices, metavar parameters of argparse.BooleanOptionalAction (#103678)
Co-authored-by: Kirill <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Oleg Iarygin <[email protected]>
1 parent ac56a85 commit 27a7d5e

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

Doc/whatsnew/3.12.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,11 @@ Pending Removal in Python 3.14
870870
* The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
871871
and will be removed in 3.14.
872872

873+
* The *type*, *choices*, and *metavar* parameters
874+
of :class:`!argparse.BooleanOptionalAction` are deprecated
875+
and will be removed in 3.14.
876+
(Contributed by Nikita Sobolev in :gh:`92248`.)
877+
873878
* :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
874879
now raise :exc:`DeprecationWarning`;
875880
use :func:`importlib.util.find_spec` instead.

Lib/argparse.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,16 +883,19 @@ def __call__(self, parser, namespace, values, option_string=None):
883883
raise NotImplementedError(_('.__call__() not defined'))
884884

885885

886+
# FIXME: remove together with `BooleanOptionalAction` deprecated arguments.
887+
_deprecated_default = object()
888+
886889
class BooleanOptionalAction(Action):
887890
def __init__(self,
888891
option_strings,
889892
dest,
890893
default=None,
891-
type=None,
892-
choices=None,
894+
type=_deprecated_default,
895+
choices=_deprecated_default,
893896
required=False,
894897
help=None,
895-
metavar=None):
898+
metavar=_deprecated_default):
896899

897900
_option_strings = []
898901
for option_string in option_strings:
@@ -902,6 +905,24 @@ def __init__(self,
902905
option_string = '--no-' + option_string[2:]
903906
_option_strings.append(option_string)
904907

908+
# We need `_deprecated` special value to ban explicit arguments that
909+
# match default value. Like:
910+
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
911+
for field_name in ('type', 'choices', 'metavar'):
912+
if locals()[field_name] is not _deprecated_default:
913+
warnings._deprecated(
914+
field_name,
915+
"{name!r} is deprecated as of Python 3.12 and will be "
916+
"removed in Python {remove}.",
917+
remove=(3, 14))
918+
919+
if type is _deprecated_default:
920+
type = None
921+
if choices is _deprecated_default:
922+
choices = None
923+
if metavar is _deprecated_default:
924+
metavar = None
925+
905926
super().__init__(
906927
option_strings=_option_strings,
907928
dest=dest,

Lib/test/test_argparse.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,49 @@ def test_const(self):
765765

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

768+
def test_deprecated_init_kw(self):
769+
# See gh-92248
770+
parser = argparse.ArgumentParser()
771+
772+
with self.assertWarns(DeprecationWarning):
773+
parser.add_argument(
774+
'-a',
775+
action=argparse.BooleanOptionalAction,
776+
type=None,
777+
)
778+
with self.assertWarns(DeprecationWarning):
779+
parser.add_argument(
780+
'-b',
781+
action=argparse.BooleanOptionalAction,
782+
type=bool,
783+
)
784+
785+
with self.assertWarns(DeprecationWarning):
786+
parser.add_argument(
787+
'-c',
788+
action=argparse.BooleanOptionalAction,
789+
metavar=None,
790+
)
791+
with self.assertWarns(DeprecationWarning):
792+
parser.add_argument(
793+
'-d',
794+
action=argparse.BooleanOptionalAction,
795+
metavar='d',
796+
)
797+
798+
with self.assertWarns(DeprecationWarning):
799+
parser.add_argument(
800+
'-e',
801+
action=argparse.BooleanOptionalAction,
802+
choices=None,
803+
)
804+
with self.assertWarns(DeprecationWarning):
805+
parser.add_argument(
806+
'-f',
807+
action=argparse.BooleanOptionalAction,
808+
choices=(),
809+
)
810+
768811
class TestBooleanOptionalActionRequired(ParserTestCase):
769812
"""Tests BooleanOptionalAction required"""
770813

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate ``type``, ``choices``, and ``metavar`` parameters of
2+
``argparse.BooleanOptionalAction``.

0 commit comments

Comments
 (0)