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

[3.15] GH-148468: Accept string-like proxy objects in colorized argparse help (GH-154653) by miss-islington · Pull Request #154946 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/argparse.py
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
Expand Up @@ -711,7 +711,7 @@ def _format_args(self, action, default_metavar):
return result

def _expand_help(self, action):
help_string = self._get_help_string(action)
help_string = str(self._get_help_string(action))
if '%' not in help_string:
return self._apply_text_markup(help_string)
params = dict(vars(action), prog=self._prog)
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_argparse.py
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
Expand Up @@ -7893,6 +7893,41 @@ def test_backtick_markup_in_argument_help_color_disabled(self):
self.assertIn("set the `foo` value", help_text)
self.assertNotIn("\x1b[", help_text)

def test_argument_help_interpolation_accepts_string_like_proxy(self):
class LazyStr:
def __init__(self, message):
self._message = message

def __str__(self):
return self._message

def __getattr__(self, name):
return getattr(str(self), name)

def __contains__(self, item):
return item in self._message

def __mod__(self, other):
return self._message % other

parser = argparse.ArgumentParser(prog="PROG", color=True)
parser.add_argument(
"--foo",
default="bar",
help=LazyStr("foo (default: %(default)s)"),
)
parser.add_argument(
"--baz",
help=LazyStr("baz plain text"),
)

interp = self.theme.interpolated_value
reset = self.theme.reset

help_text = parser.format_help()
self.assertIn(f"foo (default: {interp}bar{reset})", help_text)
self.assertIn("baz plain text", help_text)

def test_help_with_format_specifiers(self):
# GH-142950: format specifiers like %x should work with color=True
parser = argparse.ArgumentParser(prog='PROG', color=True)
Expand Down
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 a regression in :mod:`argparse` where colorized argument help containing format specifiers did not accept string-like proxy objects.
Loading

Back | FazBrowse Home | New Git URL