Skip to content

Commit 0ad1732

Browse files
authored
bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707)
1 parent 1cf8424 commit 0ad1732

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

Doc/library/argparse.rst

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,9 @@ The :meth:`~ArgumentParser.add_argument` method must know whether an optional
722722
argument, like ``-f`` or ``--foo``, or a positional argument, like a list of
723723
filenames, is expected. The first arguments passed to
724724
:meth:`~ArgumentParser.add_argument` must therefore be either a series of
725-
flags, or a simple argument name. For example, an optional argument could
726-
be created like::
725+
flags, or a simple argument name.
726+
727+
For example, an optional argument could be created like::
727728

728729
>>> parser.add_argument('-f', '--foo')
729730

@@ -765,8 +766,9 @@ how the command-line arguments should be handled. The supplied actions are:
765766
Namespace(foo='1')
766767

767768
* ``'store_const'`` - This stores the value specified by the const_ keyword
768-
argument. The ``'store_const'`` action is most commonly used with
769-
optional arguments that specify some sort of flag. For example::
769+
argument; note that the const_ keyword argument defaults to ``None``. The
770+
``'store_const'`` action is most commonly used with optional arguments that
771+
specify some sort of flag. For example::
770772

771773
>>> parser = argparse.ArgumentParser()
772774
>>> parser.add_argument('--foo', action='store_const', const=42)
@@ -795,8 +797,8 @@ how the command-line arguments should be handled. The supplied actions are:
795797
Namespace(foo=['1', '2'])
796798

797799
* ``'append_const'`` - This stores a list, and appends the value specified by
798-
the const_ keyword argument to the list. (Note that the const_ keyword
799-
argument defaults to ``None``.) The ``'append_const'`` action is typically
800+
the const_ keyword argument to the list; note that the const_ keyword
801+
argument defaults to ``None``. The ``'append_const'`` action is typically
800802
useful when multiple arguments need to store constants to the same list. For
801803
example::
802804

@@ -979,17 +981,20 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
979981
``action='store_const'`` or ``action='append_const'``. These actions add the
980982
``const`` value to one of the attributes of the object returned by
981983
:meth:`~ArgumentParser.parse_args`. See the action_ description for examples.
984+
If ``const`` is not provided to :meth:`~ArgumentParser.add_argument`, it will
985+
receive a default value of ``None``.
986+
982987

983988
* When :meth:`~ArgumentParser.add_argument` is called with option strings
984989
(like ``-f`` or ``--foo``) and ``nargs='?'``. This creates an optional
985990
argument that can be followed by zero or one command-line arguments.
986991
When parsing the command line, if the option string is encountered with no
987-
command-line argument following it, the value of ``const`` will be assumed instead.
988-
See the nargs_ description for examples.
989-
990-
With the ``'store_const'`` and ``'append_const'`` actions, the ``const``
991-
keyword argument must be given. For other actions, it defaults to ``None``.
992+
command-line argument following it, the value of ``const`` will be assumed to
993+
be ``None`` instead. See the nargs_ description for examples.
992994

995+
.. versionchanged:: 3.11
996+
``const=None`` by default, including when ``action='append_const'`` or
997+
``action='store_const'``.
993998

994999
default
9951000
^^^^^^^

Lib/argparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ def format_usage(self):
855855
def __call__(self, parser, namespace, values, option_string=None):
856856
raise NotImplementedError(_('.__call__() not defined'))
857857

858+
858859
class BooleanOptionalAction(Action):
859860
def __init__(self,
860861
option_strings,
@@ -936,7 +937,7 @@ class _StoreConstAction(Action):
936937
def __init__(self,
937938
option_strings,
938939
dest,
939-
const,
940+
const=None,
940941
default=None,
941942
required=False,
942943
help=None,
@@ -1031,7 +1032,7 @@ class _AppendConstAction(Action):
10311032
def __init__(self,
10321033
option_strings,
10331034
dest,
1034-
const,
1035+
const=None,
10351036
default=None,
10361037
required=False,
10371038
help=None,

Lib/test/test_argparse.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,25 @@ class TestOptionalsActionAppendWithDefault(ParserTestCase):
745745
]
746746

747747

748+
class TestConstActionsMissingConstKwarg(ParserTestCase):
749+
"""Tests that const gets default value of None when not provided"""
750+
751+
argument_signatures = [
752+
Sig('-f', action='append_const'),
753+
Sig('--foo', action='append_const'),
754+
Sig('-b', action='store_const'),
755+
Sig('--bar', action='store_const')
756+
]
757+
failures = ['-f v', '--foo=bar', '--foo bar']
758+
successes = [
759+
('', NS(f=None, foo=None, b=None, bar=None)),
760+
('-f', NS(f=[None], foo=None, b=None, bar=None)),
761+
('--foo', NS(f=None, foo=[None], b=None, bar=None)),
762+
('-b', NS(f=None, foo=None, b=None, bar=None)),
763+
('--bar', NS(f=None, foo=None, b=None, bar=None)),
764+
]
765+
766+
748767
class TestOptionalsActionAppendConst(ParserTestCase):
749768
"""Tests the append_const action for an Optional"""
750769

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
argparse actions store_const and append_const each receive a default value
2+
of None when the ``const`` kwarg is not provided. Previously, this raised a
3+
:exc:`TypeError`.

0 commit comments

Comments
 (0)