diff options
author | Jack DeVries <58614260+jdevries3133@users.noreply.github.com> | 2021-07-31 16:27:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-31 16:27:55 (GMT) |
commit | 0ad173249d287794d53e6a1fe2d58bb2adee2276 (patch) | |
tree | 29c7cf367e7407d308b692a86c202a445c74559f /Lib | |
parent | 1cf8424a62db38a041d421a46618e025bbb87f89 (diff) | |
download | cpython-0ad173249d287794d53e6a1fe2d58bb2adee2276.zip cpython-0ad173249d287794d53e6a1fe2d58bb2adee2276.tar.gz cpython-0ad173249d287794d53e6a1fe2d58bb2adee2276.tar.bz2 |
bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/argparse.py | 5 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 19 |
2 files changed, 22 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index e3a49e7..33c5d70 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -855,6 +855,7 @@ class Action(_AttributeHolder): def __call__(self, parser, namespace, values, option_string=None): raise NotImplementedError(_('.__call__() not defined')) + class BooleanOptionalAction(Action): def __init__(self, option_strings, @@ -936,7 +937,7 @@ class _StoreConstAction(Action): def __init__(self, option_strings, dest, - const, + const=None, default=None, required=False, help=None, @@ -1031,7 +1032,7 @@ class _AppendConstAction(Action): def __init__(self, option_strings, dest, - const, + const=None, default=None, required=False, help=None, diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 0994e70..93ac0c3 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -745,6 +745,25 @@ class TestOptionalsActionAppendWithDefault(ParserTestCase): ] +class TestConstActionsMissingConstKwarg(ParserTestCase): + """Tests that const gets default value of None when not provided""" + + argument_signatures = [ + Sig('-f', action='append_const'), + Sig('--foo', action='append_const'), + Sig('-b', action='store_const'), + Sig('--bar', action='store_const') + ] + failures = ['-f v', '--foo=bar', '--foo bar'] + successes = [ + ('', NS(f=None, foo=None, b=None, bar=None)), + ('-f', NS(f=[None], foo=None, b=None, bar=None)), + ('--foo', NS(f=None, foo=[None], b=None, bar=None)), + ('-b', NS(f=None, foo=None, b=None, bar=None)), + ('--bar', NS(f=None, foo=None, b=None, bar=None)), + ] + + class TestOptionalsActionAppendConst(ParserTestCase): """Tests the append_const action for an Optional""" |