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!

@manav4u

manav4u commented Nov 23, 2026

Copy link
Copy Markdown

One edge case in the new max: rows can be nonempty while every row is a SEPARATING_LINE, leaving the filtered generator empty.

from tabulate import SEPARATING_LINE, tabulate

print(tabulate([SEPARATING_LINE], headers=["tail"], tablefmt="simple"))

On base 268615a, this renders the header and separator. On head 8ee2ce1, it raises ValueError: max() iterable argument is empty during normalization. This can affect a table whose data rows were filtered out but whose section separators remain.

Using max((len(r) for r in rows if not _is_separating_line(r)), default=0) handles that case while retaining this PR's ragged-row fix. I checked one/two separators with explicit headers in simple, plain and github on Linux Python 3.10, 3.13 and 3.14: six new failures on the PR head, all six passing with that local adjustment; the original ragged-row controls still pass. The configured tests and doctests pass with the adjustment, 376 passed and 8 skipped on Python 3.13.

Grid-only separator tables already fail on the base, so I am not counting that separate behavior as a regression here. AI-assisted investigation and testing; no downstream incident claim.

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

2 participants