https://github.com/python/cpython/commit/1705e512b3098dce53991234a49636de2167efc3
commit: 1705e512b3098dce53991234a49636de2167efc3
branch: main
author: Savannah Ostrowski <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2026-10-22T09:12:00-07:00
summary:

gh-155403: Clarify argparse `default` precedence (#160064)

files:
M Doc/library/argparse.rst

diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 602743ddce3f6a..334a488d0b6223 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -789,6 +789,9 @@ how the command-line arguments should be handled. The 
supplied actions are:
     >>> parser.parse_args('--foo --bar'.split())
     Namespace(foo=True, bar=False, baz=True)
 
+  These defaults apply when the options are absent. They can be overridden
+  by default_ or argument_default_.
+
 * ``'append'`` - This appends each argument value to a list.
   It is useful for allowing an option to be specified multiple times.
   If the default value is a non-empty list, the parsed value for the option
@@ -1016,10 +1019,8 @@ default
 
 All optional arguments and some positional arguments may be omitted at the
 command line.  The ``default`` keyword argument of
-:meth:`~ArgumentParser.add_argument`, whose value defaults to ``None``,
-specifies what value should be used if the command-line argument is not 
present.
-For optional arguments, the ``default`` value is used when the option string
-was not present at the command line::
+:meth:`~ArgumentParser.add_argument` specifies what value should be used if the
+command-line argument is not present. For example::
 
    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument('--foo', default=42)
@@ -1028,6 +1029,11 @@ was not present at the command line::
    >>> parser.parse_args([])
    Namespace(foo=42)
 
+If ``default`` is omitted, :meth:`~ArgumentParser.add_argument` uses a value
+previously set for the dest_ with :meth:`~ArgumentParser.set_defaults`, or
+otherwise a non-``None`` argument_default_. If neither is set, the action's
+own default is used.
+
 If the target namespace already has an attribute set, the action *default*
 will not overwrite it::
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to