From fadbdbe1b911064e14cc7087c644b17530bbf7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Sat, 6 Jun 2020 00:00:42 +0200 Subject: [PATCH] bpo-40862: Raise TypeError when const is given to argparse.BooleanOptionalAction (GH-20623) (cherry picked from commit b084d1b97e369293d2d2bc0791e2135822c923a8) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Lapeyre --- Lib/argparse.py | 1 - Lib/test/test_argparse.py | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 2677ef63e9e541..2fb1da59f942cf 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -857,7 +857,6 @@ class BooleanOptionalAction(Action): def __init__(self, option_strings, dest, - const=None, default=None, type=None, choices=None, diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index e82a0c39c21a8b..22cae626ccc297 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -700,6 +700,14 @@ class TestBooleanOptionalAction(ParserTestCase): ('--no-foo --foo', NS(foo=True)), ] + def test_const(self): + # See bpo-40862 + parser = argparse.ArgumentParser() + with self.assertRaises(TypeError) as cm: + parser.add_argument('--foo', const=True, action=argparse.BooleanOptionalAction) + + self.assertIn("got an unexpected keyword argument 'const'", str(cm.exception)) + class TestBooleanOptionalActionRequired(ParserTestCase): """Tests BooleanOptionalAction required"""