This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse.RawTextHelpFormatter does not maintain lines separated by more than one newline
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, Elena.Oat, paul.j3, r.david.murray
Priority: normal Keywords: patch

Created on 2017-09-02 03:19 by Antony.Lee, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Screen Shot 2017-09-02 at 10.45.48.png Elena.Oat, 2017-09-02 07:46
Pull Requests
URL Status Linked Edit
PR 3272 merged Elena.Oat, 2017-09-03 19:38
PR 3428 merged python-dev, 2017-09-07 20:06
PR 3429 merged python-dev, 2017-09-07 20:39
Messages (10)
msg301158 - (view) Author: Antony Lee (Antony.Lee) * Date: 2017-09-02 03:19
The doc for argparse.RawTextHelpFormatter states that it "maintains whitespace for all sorts of help text, including argument descriptions."  But the following example (which outputs "Foo", "Bar", and "Baz", each separated by exactly one empty line) shows that paragraphs separated by more than one empty line will only be separated in the output text by a single empty line.


import argparse
parser = argparse.ArgumentParser(
    prog='PROG',
    formatter_class=argparse.RawTextHelpFormatter,
    description='''\
Foo

Bar


Baz''')
parser.print_help()
msg301161 - (view) Author: Elena Oat (Elena.Oat) * Date: 2017-09-02 07:46
It maintains *whitespace*, not the new lines. 
I've tested this using whitespaces and I think it keeps them according to the original description. See the image attached.
msg301162 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-02 14:22
If that is the intended definition of "whitespace" in this context (I have no opinion on that so far), then the docs need amplification, because in general "whitespace" includes newlines.  On the other hand, this might be considered a bug in the code and not the docs.  However, changing it has backward compatibility implications, so we might go for a doc fix regardless.
msg301163 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2017-09-02 15:48
I've seen this before, either in another bug/issue or a Stackoverflow question.

The basic change in RawTextHelpFormatter is to turn off the line wrapping for texts like the description.  Multiple newlines are preserved at this step.

But after assembling all the pieces (usage, description, help, epilog) the formatter strips out duplicate newlines.  This is needed because the process of assembly freely inserts newlines between the pieces, regardless of whether they are blank or not.  It can't, or at least doesn't try to, distinguish between newlines within a block like the description, and newlines between blocks.

If someone could come up with a simple patch, great.  Or a simple change to the doc wording.  But I don't think this issue merits a larger fix.
msg301164 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2017-09-02 16:19
http://bugs.python.org/issue17113
argparse.RawDescriptionHelpFormatter should not delete blank lines

This provides more details on why this is happening.

I suggest using adding a space between newlines, thus: '\n \n'.  HelpFormatter.format_help is just looking for '\n\n'.
msg301465 - (view) Author: Elena Oat (Elena.Oat) * Date: 2017-09-06 12:19
I've created a pull request. Could someone review it please?
msg301623 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-07 20:06
New changeset 397c467c49385023de36411194d381ac993bae1a by R. David Murray (Elena Oat) in branch 'master':
bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (#3272)
https://github.com/python/cpython/commit/397c467c49385023de36411194d381ac993bae1a
msg301624 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-07 20:08
Thanks, Elena.
msg301638 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-07 22:26
New changeset 82cae7c5be4175e2173e4d342825b5315a9d612a by R. David Murray (Miss Islington (bot)) in branch '2.7':
[2.7] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) (GH-3428)
https://github.com/python/cpython/commit/82cae7c5be4175e2173e4d342825b5315a9d612a
msg301640 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-07 22:26
New changeset e89b35dd2b87e85978b91e3e2dbdea1fc76d6be4 by R. David Murray (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31330: Clarify that RawTextHelpFormatter collapses repeated newlines. (GH-3272) (GH-3429)
https://github.com/python/cpython/commit/e89b35dd2b87e85978b91e3e2dbdea1fc76d6be4
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75511
2017-09-07 23:18:33r.david.murraysetstatus: open -> closed
stage: patch review -> resolved
2017-09-07 22:26:42r.david.murraysetmessages: + msg301640
2017-09-07 22:26:30r.david.murraysetmessages: + msg301638
2017-09-07 20:39:46python-devsetkeywords: + patch
stage: backport needed -> patch review
pull_requests: + pull_request3426
2017-09-07 20:08:09r.david.murraysetresolution: fixed
messages: + msg301624
stage: backport needed
2017-09-07 20:06:59python-devsetpull_requests: + pull_request3425
2017-09-07 20:06:49r.david.murraysetmessages: + msg301623
2017-09-06 12:19:26Elena.Oatsetmessages: + msg301465
2017-09-03 19:38:10Elena.Oatsetpull_requests: + pull_request3316
2017-09-02 16:19:07paul.j3setmessages: + msg301164
2017-09-02 15:48:45paul.j3setmessages: + msg301163
2017-09-02 14:22:48r.david.murraysetnosy: + r.david.murray, paul.j3

messages: + msg301162
versions: + Python 3.7
2017-09-02 07:46:16Elena.Oatsetfiles: + Screen Shot 2017-09-02 at 10.45.48.png
nosy: + Elena.Oat
messages: + msg301161

2017-09-02 03:19:37Antony.Leecreate