Visitar URL original
gh-96859: Optimize argparse actions 'append', 'append_const' and 'extend' by serhiy-storchaka · Pull Request #124909 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260801211349/https://github.com/python/cpython/pull/124909
Skip to content
/ cpython Public

gh-96859: Optimize argparse actions 'append', 'append_const' and 'extend' - #124909

Draft
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:argparse-copy_items-optimize
Draft

gh-96859: Optimize argparse actions 'append', 'append_const' and 'extend'#124909
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:argparse-copy_items-optimize

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Oct 2, 2024 •

Copy link
Copy Markdown
Member

Previously, the target list was copied every time the action was taken. Now only the default value is copied the first time the action is taken.

…nd 'extend'

Previously, the target list was copied every time the action was taken.
Now only the default value is copied the first time the action is taken.

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The title of the PR should be changed to gh-96859: Optimize ... to link to a github issue. (gh-124745 is the number of an alternative PR)

Comment thread Misc/NEWS.d/next/Library/2024-10-02-21-34-08.gh-issue-124745.efyaxu.rst Outdated
Comment thread Lib/argparse.py
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest, None)
items = _copy_items(items)
if items is self.default:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As far as I can tell this changes the behavior in some corner cases. For example, when parse_known_args() is called with a not empty namespace. The new code will only clone lists when default is set. The old code would have cloned any existing list. This matters if the caller has another reference to the same list.

Something like this

some_existing_list = []
ns = argparse.Namespace()
ns.setattr("foo", some_existing_list)

parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='append')
...

... = parser.parse_known_args(args=['--foo=xxx'], namespace=ns)

# At this point some_existing_list is ['xxx']

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point. There are no tests for this (and some other corner cases).

@serhiy-storchaka serhiy-storchaka changed the title gh-124745: Optimize argparse actions 'append', 'append_const' and 'extend' gh-96859: Optimize argparse actions 'append', 'append_const' and 'extend' Oct 3, 2024
Comment thread Misc/NEWS.d/next/Library/2024-10-02-21-34-08.gh-issue-96859.efyaxu.rst Outdated
…yaxu.rst

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 18, 2026
@savannahostrowski

Copy link
Copy Markdown
Member

@serhiy-storchaka I'm going through stale PRs/pending issues for argparse. I closed the alternative approach that preceded this PR draft.

My preference would be to not change semantics here but I'm wondering if you still plan to work on this and finish it up? If not, maybe we should close this out too.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

argparse.ArgumentParser is slow when parsing a very long option list

5 participants