Skip to content

Do not drop columns when headers are shorter than ragged rows (#494) - #504

Open
Rajpal14 wants to merge 1 commit into
astanin:masterfrom
Rajpal14:fix/ragged-table-headers
Open

Rajpal14 wants to merge 1 commit into
astanin:masterfrom
Rajpal14:fix/ragged-table-headers

Conversation

@Rajpal14

@Rajpal14 Rajpal14 commented Nov 9, 2026

Copy link
Copy Markdown

Problem

Fixes #494.

When tabular data contains rows of varying lengths (ragged rows) and the first row has fewer columns than subsequent rows, _normalize_tabular_data computed headers_pad using only len(rows[0]):

headers_pad = max(0, len(rows[0]) - len(headers))

If headers was shorter than the maximum column count across all data rows (e.g. tabulate([["a"], ["b", "KEEP"]], headers=["tail"])), headers remained shorter than the total number of columns. Later, zip(cols, aligns_copy, minwidths) truncated the columns to the length of minwidths (derived from headers), silently dropping data columns like "KEEP".

Solution

  • Updated _normalize_tabular_data to compute headers_pad using the maximum column count across all non-separating data rows (max(len(r) for r in rows if not _is_separating_line(r))).
  • Added a regression test test_ragged_rows_with_short_headers in test/test_regression.py to verify that columns beyond the first row's width are preserved when short headers are supplied.

Verification

…n#494)

When tabular data contains rows of varying lengths (ragged rows) and the first
row has fewer columns than subsequent rows, _normalize_tabular_data previously
computed headers_pad using only len(rows[0]). When headers was shorter than the
maximum row length, the headers list remained shorter than the total number of
columns, causing zip(cols, aligns, minwidths) to truncate the table columns.

Now headers_pad uses the maximum column count across all non-separating data rows,
ensuring all table columns are properly preserved and aligned.

Fixes astanin#494
@Rajpal14
Rajpal14 force-pushed the fix/ragged-table-headers branch from 82d4bd1 to 8ee2ce1 Compare November 9, 2026 20:45
@Rajpal14

Copy link
Copy Markdown
Author

Hi maintainers, when you have time, could you please review this fix for #494? It includes a regression test for ragged rows with shorter headers, and the full test suite passes locally. Thank you!

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.

Short headers silently drop columns from ragged tables

1 participant