From 3ed730b688c84555c8eef26c31ef3f68a6202f6a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 9 May 2024 01:06:36 +0300 Subject: [PATCH 1/3] gh-118805: Remove type, choices, metavar params of `BooleanOptionalAction` --- Doc/whatsnew/3.14.rst | 3 ++ Lib/argparse.py | 28 ------------ Lib/test/test_argparse.py | 43 ------------------- ...-05-09-01-05-52.gh-issue-118805.N7dm07.rst | 2 + 4 files changed, 5 insertions(+), 71 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 6a9d0b0d912fb2..261c83e3b38eb6 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -105,6 +105,9 @@ Removed It had previously raised a :exc:`DeprecationWarning` since Python 3.9. (Contributed by Jelle Zijlstra in :gh:`118767`.) +* The *type*, *choices*, and *metavar* parameters + of :class:`!argparse.BooleanOptionalAction` are removed. + They were deprecated since 3.12. Porting to Python 3.14 ====================== diff --git a/Lib/argparse.py b/Lib/argparse.py index 55bf8cdd875a8d..cdd29d3ad568e5 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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 = [] @@ -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) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 02b499145f6c43..eb1a9f5146beb4 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -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""" diff --git a/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst b/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst new file mode 100644 index 00000000000000..870ebc8d561e67 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst @@ -0,0 +1,2 @@ +Remove ``type``, ``choices``, and ``metavar`` parameters of +:class:`!argparse.BooleanOptionalAction`. From 7e2f39995aaeb633dffded5dbf29fc4525b50426 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 9 May 2024 12:44:42 +0300 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Alex Waygood --- .../Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst b/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst index 870ebc8d561e67..4f1db04d8bd67f 100644 --- a/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst +++ b/Misc/NEWS.d/next/Library/2024-05-09-01-05-52.gh-issue-118805.N7dm07.rst @@ -1,2 +1,3 @@ -Remove ``type``, ``choices``, and ``metavar`` parameters of +Remove *type*, *choices*, and *metavar* parameters of :class:`!argparse.BooleanOptionalAction`. +They were deprecated since Python 3.12. From 3109dafcf9e35c3689746e59e988c76025fbfe4c Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 9 May 2024 14:24:45 +0300 Subject: [PATCH 3/3] Address review --- Doc/whatsnew/3.14.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index b67a55ae64d998..25c43dc0387eaf 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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 ----- @@ -118,10 +125,6 @@ Others are removed. They had previously raised a :exc:`DeprecationWarning` since Python 3.12. -* The *type*, *choices*, and *metavar* parameters - of :class:`!argparse.BooleanOptionalAction` are removed. - They were deprecated since 3.12. - * :mod:`itertools` support for copy, deepcopy, and pickle operations. These had previously raised a :exc:`DeprecationWarning` since Python 3.12. (Contributed by Raymond Hettinger in :gh:`101588`.)