click icon indicating copy to clipboard operation
click copied to clipboard

[rejected AI] `HelpFormatter.write_dl`: raises `TypeError: Expected two columns for definition list` on empty sequence

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

Outline

Calling HelpFormatter.write_dl(rows) with an empty sequence rows = [] crashes with TypeError: Expected two columns for definition list. An empty definition list should be a valid no-op. Because write_dl lacks an empty-sequence guard, internal callers (format_options, format_commands) and third-party extensions are forced to maintain redundant if records: guards to avoid crashing.

Steps to Reproduce

from click.formatting import HelpFormatter

formatter = HelpFormatter()
formatter.write_dl([])

Traceback

Traceback (most recent call last):
  File "<string>", line 4, in <module>
    formatter.write_dl([])
  File "src/click/formatting.py", line 246, in write_dl
    raise TypeError("Expected two columns for definition list")
TypeError: Expected two columns for definition list

Expected Behavior

write_dl([]) should return cleanly without appending to the buffer.

Root Cause Analysis

In src/click/formatting.py, lines 243–246:

rows = list(rows)
widths = measure_table(rows)
if len(widths) != 2:
    raise TypeError("Expected two columns for definition list")

When rows is empty, measure_table(rows) returns () (length 0). The check if len(widths) != 2 erroneously raises TypeError instead of allowing an empty list of rows to complete gracefully.

Proposed Fix

--- a/src/click/formatting.py
+++ b/src/click/formatting.py
@@ -241,6 +247,8 @@ class HelpFormatter:
                             second column.
         """
         rows = list(rows)
+        if not rows:
+            return
         widths = measure_table(rows)
         if len(widths) != 2:
             raise TypeError("Expected two columns for definition list")

Regression Test

def test_write_dl_empty_rows():
    from click.formatting import HelpFormatter
    formatter = HelpFormatter()
    formatter.write_dl([])
    assert formatter.getvalue() == ""

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