@@ -7898,6 +7898,41 @@ def test_backtick_markup_in_argument_help_color_disabled(self): |
7898 | 7898 | self.assertIn("set the `foo` value", help_text) |
7899 | 7899 | self.assertNotIn("\x1b[", help_text) |
7900 | 7900 |
|
| 7901 | + def test_argument_help_interpolation_accepts_string_like_proxy(self): |
| 7902 | + class LazyStr: |
| 7903 | + def __init__(self, message): |
| 7904 | + self._message = message |
| 7905 | + |
| 7906 | + def __str__(self): |
| 7907 | + return self._message |
| 7908 | + |
| 7909 | + def __getattr__(self, name): |
| 7910 | + return getattr(str(self), name) |
| 7911 | + |
| 7912 | + def __contains__(self, item): |
| 7913 | + return item in self._message |
| 7914 | + |
| 7915 | + def __mod__(self, other): |
| 7916 | + return self._message % other |
| 7917 | + |
| 7918 | + parser = argparse.ArgumentParser(prog="PROG", color=True) |
| 7919 | + parser.add_argument( |
| 7920 | + "--foo", |
| 7921 | + default="bar", |
| 7922 | + help=LazyStr("foo (default: %(default)s)"), |
| 7923 | + ) |
| 7924 | + parser.add_argument( |
| 7925 | + "--baz", |
| 7926 | + help=LazyStr("baz plain text"), |
| 7927 | + ) |
| 7928 | + |
| 7929 | + interp = self.theme.interpolated_value |
| 7930 | + reset = self.theme.reset |
| 7931 | + |
| 7932 | + help_text = parser.format_help() |
| 7933 | + self.assertIn(f"foo (default: {interp}bar{reset})", help_text) |
| 7934 | + self.assertIn("baz plain text", help_text) |
| 7935 | + |
7901 | 7936 | def test_help_with_format_specifiers(self): |
7902 | 7937 | # GH-142950: format specifiers like %x should work with color=True |
7903 | 7938 | parser = argparse.ArgumentParser(prog='PROG', color=True) |
|