Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:

# Description
if variable in self.class_variables and (comment := self.class_variables[variable]["comment"]):
kwargs["help"] += " " + comment
# Escape "%" so a literal percent in a comment doesn't crash argparse's "%"-formatting of the help string.
kwargs["help"] += " " + comment.replace("%", "%%")

# Set other kwargs where not provided
if variable in self._annotations:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_integration.py

@martinjm97 martinjm97 Jul 11, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test would benefit from a full string match to ensure the parse is actually "The % chance this will break" and not mangled.

Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class ParseTwiceTap(Tap):
with self.assertRaises(ValueError):
ParseTwiceTap().parse_args(["--a", "6"]).parse_args(["--a", "7"])

def test_comment_with_percent(self) -> None:
# A '%' in a class-variable comment must not crash argparse's '%' formatting of the help string (#183).
class PercentCommentTap(Tap):
percent: int = 100 # The % chance this will break

# On Python <3.14 the crash surfaces when argparse %-formats the help; on 3.14 it
# surfaces eagerly during construction. Either way this must not raise.
help_text = PercentCommentTap().format_help()
self.assertIn("chance this will break", help_text)


class RequiredClassVariableTests(TestCase):
def setUp(self) -> None:
Expand Down
Loading