click icon indicating copy to clipboard operation
click copied to clipboard

[rejected AI] `wrap_text` collapses paragraphs into a single line when blank lines contain whitespace

Open codeCraft-Ritik opened this issue 1 month ago • 1 comments

Outline

When formatting docstrings or command help texts with wrap_text(text, preserve_paragraphs=True), empty lines that contain whitespace characters (e.g. trailing spaces or tabs on an otherwise blank line) fail to trigger paragraph flushing. Instead of separating paragraphs, Click treats the whitespace line as an indented continuation of the active paragraph, strips the line to "", and joins the surrounding paragraphs with a space. This causes multi-paragraph documentation to be collapsed into a single run-on paragraph with extraneous spacing.

Steps to Reproduce

Run the following script:

from click.formatting import wrap_text

# Blank line contains 3 spaces
help_text = "First paragraph of instructions.\n   \nSecond paragraph of instructions."
wrapped = wrap_text(help_text, preserve_paragraphs=True)

print("Result:\n" + repr(wrapped))
assert "\n\n" in wrapped, "Paragraph separation was lost!"

Traceback / Output

Result:
'First paragraph of instructions.     Second paragraph of instructions.'
Traceback (most recent call last):
  File "test_wrap.py", line 8, in <module>
    assert "\n\n" in wrapped, "Paragraph separation was lost!"
AssertionError: Paragraph separation was lost!

Expected Behavior

Lines containing only whitespace should be recognized as paragraph boundaries. The output should preserve the double newline between paragraphs:

'First paragraph of instructions.\n\nSecond paragraph of instructions.'

Root Cause Analysis

In src/click/formatting.py, lines 87–96:

for line in text.splitlines():
    if not line:
        _flush_par()
        indent = None
    else:
        if indent is None:
            orig_len = term_len(line)
            line = line.lstrip()
            indent = orig_len - term_len(line)
        buf.append(line)
_flush_par()

When line consists only of spaces (e.g. " "), if not line: evaluates to False. The loop enters the else: branch, measures indent, sets line = "" via line.lstrip(), and appends "" to buf. Because buf is not flushed, the next paragraph's lines are appended to the same buffer and joined with " ".

Proposed Fix

--- a/src/click/formatting.py
+++ b/src/click/formatting.py
@@ -88,6 +88,12 @@ def wrap_text(
         if not line:
             _flush_par()
             indent = None
+        elif line.isspace():
+            if buf:
+                _flush_par()
+                indent = None
+            else:
+                buf.append("")
         else:
             if indent is None:
                 orig_len = term_len(line)

Regression Test

def test_wrap_text_whitespace_blank_lines():
    from click.formatting import wrap_text
    text = "First paragraph.\n   \nSecond paragraph."
    wrapped = wrap_text(text, preserve_paragraphs=True)
    assert "\n\n" in wrapped
    assert wrapped.split("\n\n") == ["First paragraph.", "Second paragraph."]

Environment

  • Python version: 3.10 / 3.11 / 3.12 / 3.13 / 3.14
  • Click version: 8.5.x / 8.6.0a0 (main)

codeCraft-Ritik avatar Oct 23 '26 04:10 codeCraft-Ritik

https://palletsprojects.com/contributing/llm-ai

davidism avatar Oct 23 '26 04:10 davidism