Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for argparse's append action #131389

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

moshekaplan
Copy link
Contributor

@moshekaplan moshekaplan commented Mar 18, 2025

Add an example for argparse's append action when a default value is provided.


📚 Documentation preview 📚: https://cpython-previews--131389.org.readthedocs.build/

Add an example for `argparse`'s `append` action when a default value is provided.
@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Mar 18, 2025
Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need it? Maybe both could be combined? The provided explanation is clear to me:

If the default value is non-empty, the default elements will be present
in the parsed value for the option, with any values from the
command line appended after those default values.

@moshekaplan
Copy link
Contributor Author

The issue that drove this PR is that until I tested it, it was unclear that the default needed to be a list, and it couldn't be a single value. While this may be obvious in hindsight, I realized that a short example would entirely remove this source of confusion.

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append', default='0')
_AppendAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default='0', type=None, choices=None, required=False, help=None, metavar=None)
>>> parser.parse_args('--foo 1 --foo 2'.split())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Python\Python311\Lib\argparse.py", line 1862, in parse_args
    args, argv = self.parse_known_args(args, namespace)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Python\Python311\Lib\argparse.py", line 1895, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Python\Python311\Lib\argparse.py", line 2107, in _parse_known_args
    start_index = consume_optional(start_index)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Python\Python311\Lib\argparse.py", line 2047, in consume_optional
    take_action(action, args, option_string)
  File "Python\Python311\Lib\argparse.py", line 1971, in take_action
    action(self, namespace, argument_values, option_string)
  File "Python\Python311\Lib\argparse.py", line 1043, in __call__
    items.append(values)
    ^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'append'

@StanFromIreland
Copy link
Contributor

I see, you are correct, that should be clarified, but not as an example in my opinion as it is not very clear, and the text remains not clear.

@moshekaplan
Copy link
Contributor Author

Unfortunately, there are several other parts of the argparse documentation which I think could be much clearer. Would it be valuable to make a separate PR for those?

@savannahostrowski
Copy link
Member

Hey, thanks for the PR. I recommend adding a note in the describing paragraph that this should be a list instead. The code example is a bit duplicative as is.

If you have other ideas for documentation improvements, you're welcome to open issues so we can discuss the changes/areas where you think things could be clearer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants