* The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
and will be removed in 3.14.
+* The *type*, *choices*, and *metavar* parameters
+ of :class:`!argparse.BooleanOptionalAction` are deprecated
+ and will be removed in 3.14.
+ (Contributed by Nikita Sobolev in :gh:`92248`.)
+
* :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
now raise :exc:`DeprecationWarning`;
use :func:`importlib.util.find_spec` instead.
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:
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
+
super().__init__(
option_strings=_option_strings,
dest=dest,
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"""
--- /dev/null
+Deprecate ``type``, ``choices``, and ``metavar`` parameters of
+``argparse.BooleanOptionalAction``.