Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-142950: Process format specifiers before colourization in argparse help#142960
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
Merged
savannahostrowski merged 8 commits into python:main from savannahostrowski:argparse-format-specifierJan 7, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
b3deacc Format before colourization
savannahostrowski 987af8b Clean up
savannahostrowski 2e73cd5 Comments
savannahostrowski 8fc4cd2 📜🤖 Added by blurb_it.
blurb-it[bot] 62ceb27 Lint
savannahostrowski e599afa Address additional cases
savannahostrowski 95919d9 Merge branch 'main' into argparse-format-specifier
savannahostrowski 4d13744 Merge branch 'main' into argparse-format-specifier
savannahostrowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -688,11 +688,41 @@ def _expand_help(self, action): | ||
| params[name] = value.__name__ | ||
| if params.get('choices') is not None: | ||
| params['choices'] = ', '.join(map(str, params['choices'])) | ||
| # Before interpolating, wrap the values with color codes | ||
| t = self._theme | ||
| for name, value in params.items(): | ||
| params[name] = f"{t.interpolated_value}{value}{t.reset}" | ||
| return help_string % params | ||
| result = help_string % params | ||
| if not t.reset: | ||
| return result | ||
| # Match format specifiers like: %s, %d, %(key)s, etc. | ||
| fmt_spec = r''' | ||
| % | ||
| (?: | ||
| % # %% escape | ||
| | | ||
| (?:\((?P<key>[^)]*)\))? # key | ||
| [-#0\ +]* # flags | ||
| (?:\*|\d+)? # width | ||
| (?:\.(?:\*|\d+))? # precision | ||
| [hlL]? # length modifier | ||
| [diouxXeEfFgGcrsa] # conversion type | ||
| ) | ||
| ''' | ||
savannahostrowski marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| def colorize(match): | ||
| spec, key = match.group(0, 'key') | ||
| if spec == '%%': | ||
| return '%' | ||
| if key is not None: | ||
| # %(key)... - format and colorize | ||
| formatted = spec %{key: params[key]} | ||
| return f'{t.interpolated_value}{formatted}{t.reset}' | ||
| # bare %s etc. - format with full params dict, no colorization | ||
| return spec % params | ||
| return _re.sub(fmt_spec, colorize, help_string, flags=_re.VERBOSE) | ||
| def _iter_indented_subactions(self, action): | ||
| try: | ||
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
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2025-12-18-22-58-46.gh-issue-142950.EJ8w-T.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix regression in :mod:`argparse` where format specifiers in help strings raised :exc:`ValueError`. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.