FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-61082: Clarify nargs='*' positional default behavior by savannahostrowski · Pull Request #150989 · python/cpython · GitHub

/ cpython Public

GH-61082: Clarify nargs='*' positional default behavior - #150989

Merged
savannahostrowski merged 3 commits into
python:mainfrom
savannahostrowski:gh-61082-nargs-star-default-doc
Jun 9, 2026
Merged

GH-61082: Clarify nargs='*' positional default behavior#150989
savannahostrowski merged 3 commits into
python:mainfrom
savannahostrowski:gh-61082-nargs-star-default-doc

Conversation

savannahostrowski commented Jun 5, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

savannahostrowski added docs Documentation in the Doc dir skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 5, 2026
github-project-automation Bot moved this to Todo in Docs PRs Jun 5, 2026

read-the-docs-community Bot commented Jun 5, 2026
edited
Loading

Copy link
Copy Markdown

savannahostrowski linked an issue Jun 9, 2026 that may be closed by this pull request
Comment thread Doc/library/argparse.rst
Comment on lines +1055 to +1058
Because ``nargs='*'`` gathers any supplied values into a list, an absent
positional argument yields an empty list (``[]``). Only a non-``None``
*default* overrides this (so ``default=None`` still gives ``[]``).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I tested the nargs="*" cases in the REPL to make sure I followed it: with default=None it returns an empty list and a non-None default overrides it. The note reads clearly to me.

>>> import argparse
>>> p = argparse.ArgumentParser()
>>> p.add_argument("foo", nargs="*", default=None)
_StoreAction(option_strings=[], dest='foo', nargs='*', const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> p.parse_args([])
Namespace(foo=[])
>>> p2 = argparse.ArgumentParser()
>>> p2.add_argument("foo", nargs="*", default=[1, 2, 3])
_StoreAction(option_strings=[], dest='foo', nargs='*', const=None, default=[1, 2, 3], type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
>>> p2.parse_args([])
Namespace(foo=[1, 2, 3])

savannahostrowski enabled auto-merge (squash) June 9, 2026 15:28
savannahostrowski merged commit bc37a22 into python:main Jun 9, 2026
30 checks passed
github-project-automation Bot moved this from Todo to Done in Docs PRs Jun 9, 2026

Copy link
Copy Markdown

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151168 is a backport of this pull request to the 3.15 branch.

bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 9, 2026

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151169 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 9, 2026

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151170 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 9, 2026
savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…0989) (#151170)

GH-61082: Clarify nargs='*' positional default behavior (GH-150989)
(cherry picked from commit bc37a22)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…0989) (#151169)

GH-61082: Clarify nargs='*' positional default behavior (GH-150989)
(cherry picked from commit bc37a22)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
savannahostrowski added a commit that referenced this pull request Jun 9, 2026
…0989) (#151168)

GH-61082: Clarify nargs='*' positional default behavior (GH-150989)
(cherry picked from commit bc37a22)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

argparse: positional args with nargs='*' defaults to []

2 participants


Back | FazBrowse Home | New Git URL