Skip to content

Escape '%' in help text taken from comments and docstrings - #376

Open
Mykhailo-Romanov-AMZ wants to merge 1 commit into
lebrice:masterfrom
Mykhailo-Romanov-AMZ:fix/372-percent-in-help
Open

Mykhailo-Romanov-AMZ wants to merge 1 commit into
lebrice:masterfrom
Mykhailo-Romanov-AMZ:fix/372-percent-in-help

Conversation

@Mykhailo-Romanov-AMZ

Copy link
Copy Markdown

Hi! I'm Mykhailo, a first-time contributor. Thanks for maintaining SimpleParsing.

What does this PR do?

Fixes #372 by escaping % as %% in help text taken from a field's comment or docstring, so argparse prints it literally. Explicit field(help=...) strings are unchanged, so %(default)s still works there.

How was this tested?

  • Added test case: test_help_from_docstring_with_percent_sign to cover % in a comment above, a docstring below and an inline comment, plus an explicit help= control. It fails on master with the TypeError from the issue and passes with the fix.
  • Output: passed locally via uv run pytest --benchmark-disable test/test_docstrings.py (10 passed). pre-commit run on the changed files passes.

Diff Summary

  • simple_parsing/wrappers/field_wrapper.py: escape % in comment/docstring help in get_arg_options.
  • test/test_docstrings.py: added reproduction test.

Prepared with AI assistance; I reviewed and verified it locally.

argparse treats `help` as a %-format string, so a literal '%' in a field's
comment or docstring (e.g. "Retry about 5% of requests.") made `--help`
raise a TypeError. Escape '%' as '%%' when the help comes from a comment or
docstring. Explicit `help=` strings are left unchanged, so `%(default)s`
still works there.

Fixes lebrice#372

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--help raises TypeError when a field's comment or docstring contains %

1 participant