From 552423076f27cfae56140f399ab4b23608977fbb Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 22 Apr 2023 12:34:45 +0300 Subject: [PATCH 1/3] gh-92248: Deprecate `type`, `chocies`, `metavar` parameters of `argparse.BooleanOptionalAction` --- Doc/whatsnew/3.12.rst | 6 +++ Lib/argparse.py | 27 ++++++++++-- Lib/test/test_argparse.py | 43 +++++++++++++++++++ ...3-04-22-12-30-10.gh-issue-92248.NcVTKR.rst | 2 + 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index f9406653e625b5..5d3f0c561d8095 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -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 + of :class:`!argparse.BooleanOptionalAction` are deprecated + and will be removed in 3.14 + (Contributed by Nikita Sobolev in :gh:`92248`.) + + Pending Removal in Future Versions ---------------------------------- diff --git a/Lib/argparse.py b/Lib/argparse.py index a819d2650e85f0..a4257dfa770e76 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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: @@ -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 + super().__init__( option_strings=_option_strings, dest=dest, diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 861da2326d1214..7d11a6d3f037d7 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -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""" diff --git a/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst b/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst new file mode 100644 index 00000000000000..a629b7235fa00c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst @@ -0,0 +1,2 @@ +Deprecate ``type``, ``chocies``, and ``metavar`` parameters of +``argparse.BooleanOptionalAction``. From 5df2d405c14a54401a669082c20aa9e4d187e60f Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 22 Apr 2023 12:55:37 +0300 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst Co-authored-by: Kirill <80244920+Eclips4@users.noreply.github.com> --- .../next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst b/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst index a629b7235fa00c..d4a02d82941927 100644 --- a/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst +++ b/Misc/NEWS.d/next/Library/2023-04-22-12-30-10.gh-issue-92248.NcVTKR.rst @@ -1,2 +1,2 @@ -Deprecate ``type``, ``chocies``, and ``metavar`` parameters of +Deprecate ``type``, ``choices``, and ``metavar`` parameters of ``argparse.BooleanOptionalAction``. From 808826b2019dea87038ccdda500b6c5563d1c318 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 1 May 2023 10:48:09 +0300 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Hugo van Kemenade --- Doc/whatsnew/3.12.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 5d216839fb48af..c1a65e5a9cd09b 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -730,9 +730,9 @@ 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 +* The *type*, *choices*, and *metavar* parameters of :class:`!argparse.BooleanOptionalAction` are deprecated - and will be removed in 3.14 + and will be removed in 3.14. (Contributed by Nikita Sobolev in :gh:`92248`.) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy