Skip to content

gh-149614 - Restore deepcopiability of argparse.ArgumentParser instances#149617

Open
DavidCEllis wants to merge 10 commits intopython:mainfrom
DavidCEllis:unbreak-argparse-copy
Open

gh-149614 - Restore deepcopiability of argparse.ArgumentParser instances#149617
DavidCEllis wants to merge 10 commits intopython:mainfrom
DavidCEllis:unbreak-argparse-copy

Conversation

@DavidCEllis
Copy link
Copy Markdown
Contributor

@DavidCEllis DavidCEllis commented May 9, 2026

With the creation of the _ColorlessTheme to avoid importing _colorize early the new class unfortunately was returning empty strings for __deepcopy__ along with other dunder methods which broke deepcopy.

I've added tests for copy and deepcopy based on the test for pickle.

Being more cautious, I've narrowed the class to only returning empty strings on attributes matching those taken from _colorize.

This does mean making sure the set remains in sync which could be more annoying to maintain, it would also be possible to just exclude anything starting with _. I've updated the test that checks this theme to directly check they are in sync.

Comment thread Lib/argparse.py Outdated
Comment thread Lib/test/test_argparse.py
Comment thread Lib/test/test_argparse.py
Comment thread Lib/test/test_argparse.py
Comment thread Lib/argparse.py Outdated
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thank you!

Comment thread Lib/argparse.py
def __getattr__(self, name):
# _colorize's no_color themes are just all empty strings
# by directly using empty strings the import is avoided
if name.startswith("_"):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any reason not to check that this starts and ends with __ ?

We use this pattern elsewhere in the repo as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants