From cf1e87dc806e960437aaf77ab3addefeb7de1b77 Mon Sep 17 00:00:00 2001 From: Yash Dhankhar Date: Tue, 17 Nov 2026 17:44:51 +0530 Subject: [PATCH 1/2] Preserve grid rows and alignments across separating lines Keep data rows and vertical alignments when separators are reinserted. AI-assisted implementation and tests. --- tabulate/__init__.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..83d5feb 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2637,8 +2637,9 @@ def _format_table( _append_line(lines, padded_widths, colaligns, fmt.linebelowheader) if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden: - # initial rows with a line below - for row, ralign in zip(rows[:-1], rowaligns): + # Separators have no row alignment and must not shorten the data-row iteration. + rowalign_iter = iter(rowaligns) + for index, row in enumerate(rows): if row != SEPARATING_LINE: append_row( lines, @@ -2646,18 +2647,10 @@ def _format_table( padded_widths, colaligns, fmt.datarow, - rowalign=ralign, + rowalign=next(rowalign_iter), ) - _append_line(lines, padded_widths, colaligns, fmt.linebetweenrows) - # the last row without a line below - append_row( - lines, - pad_row(rows[-1], pad), - padded_widths, - colaligns, - fmt.datarow, - rowalign=rowaligns[-1], - ) + if index < len(rows) - 1: + _append_line(lines, padded_widths, colaligns, fmt.linebetweenrows) else: separating_line = ( fmt.linebetweenrows From 65a853e15ae6ea63a23f8f4542eb7aaf1dc8f010 Mon Sep 17 00:00:00 2001 From: Yash Dhankhar Date: Tue, 17 Nov 2026 17:45:30 +0530 Subject: [PATCH 2/2] Cover grid separators, row preservation and vertical alignment Add 34 regression/control cases across grid formats. AI-assisted tests. --- test/test_regression.py | 68 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/test/test_regression.py b/test/test_regression.py index 9555676..e8c3339 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -1,6 +1,8 @@ """Regression tests.""" -from tabulate import DataRow, Line, TableFormat, tabulate +import pytest + +from tabulate import SEPARATING_LINE, DataRow, Line, TableFormat, tabulate from common import assert_equal, skip @@ -598,3 +600,67 @@ def test_github_escape_pipe_character(): result = tabulate([["foo|bar"]], headers=("spam|eggs",), tablefmt="github") expected = "| spam\\|eggs |\n|:------------|\n| foo\\|bar |" assert_equal(expected, result) + + +@pytest.mark.parametrize("tablefmt", ["grid", "fancy_grid", "rounded_grid"]) +@pytest.mark.parametrize( + "separators", + [(), (1,), (4,), (1, 3), (0, 2, 3), (1, 2, 4), (0, 1, 3, 6), (1, 5)], +) +def test_grid_separators_preserve_every_data_row(tablefmt, separators): + rows = [[label] for label in ("Alpha", "Bravo", "Charlie", "Delta")] + for index in separators: + rows.insert(index, SEPARATING_LINE) + + result = tabulate(rows, tablefmt=tablefmt) + + assert SEPARATING_LINE not in result + for label in ("Alpha", "Bravo", "Charlie", "Delta"): + assert result.count(label) == 1 + + +def test_grid_multiple_separators_output(): + rows = [["A"], SEPARATING_LINE, ["B"], SEPARATING_LINE, ["C"], ["D"]] + + assert tabulate(rows, tablefmt="grid") == "\n".join( + [ + "+---+", + "| A |", + "+---+", + "+---+", + "| B |", + "+---+", + "+---+", + "| C |", + "+---+", + "| D |", + "+---+", + ] + ) + + +@pytest.mark.parametrize("tablefmt", ["grid", "fancy_grid", "rounded_grid"]) +@pytest.mark.parametrize("alignment", ["top", "center", "bottom"]) +def test_grid_separators_do_not_consume_row_alignment(tablefmt, alignment): + rows = [ + ["A1\nA2\nA3", "alpha"], + SEPARATING_LINE, + ["B1\nB2\nB3", "bravo"], + SEPARATING_LINE, + ["C1\nC2\nC3", "charlie"], + ["D1\nD2\nD3", "delta"], + ] + rowalign = ["top", alignment, "bottom", "center"] + + result = tabulate(rows, tablefmt=tablefmt, rowalign=rowalign) + + expected_lines = { + "alpha": "A1", + "bravo": {"top": "B1", "center": "B2", "bottom": "B3"}[alignment], + "charlie": "C3", + "delta": "D2", + } + for label, expected in expected_lines.items(): + matching_lines = [line for line in result.splitlines() if label in line] + assert len(matching_lines) == 1 + assert expected in matching_lines[0]