From 8958b2281a9707a80bce19474c9836d19b0acb75 Mon Sep 17 00:00:00 2001 From: semiflux <79944820+semiflux@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:17:30 +0100 Subject: [PATCH] Fix KeyError for disabled colalign in asciidoc tables `colalign` entries set to `None` disable alignment for that column, but the asciidoc formatter looked the value up directly in its alignment map and raised KeyError. Fall back to left alignment, as the other formatters already do for missing alignment values. --- tabulate/__init__.py | 4 +++- test/test_output.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..b510ce9 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -263,7 +263,9 @@ def make_header_line(is_header, colwidths, colaligns): alignment = {"left": "<", "right": ">", "center": "^", "decimal": ">"} # use the column widths generated by tabulate for the asciidoc column width specifiers - asciidoc_alignments = zip(colwidths, [alignment[colalign] for colalign in colaligns]) + asciidoc_alignments = zip( + colwidths, [alignment.get(colalign, "<") for colalign in colaligns] + ) asciidoc_column_specifiers = [f"{align}{width:d}" for width, align in asciidoc_alignments] header_list = ['cols="' + (",".join(asciidoc_column_specifiers)) + '"'] diff --git a/test/test_output.py b/test/test_output.py index ea3da87..ad42da5 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -2152,6 +2152,24 @@ def test_asciidoc_headerless(): assert_equal(expected, result) +def test_asciidoc_with_none_colalign(): + "Output: asciidoc with disabled column alignment" + expected = "\n".join( + [ + '[cols="<11,>11",options="header"]', + "|====", + "| strings | numbers", + "| spam | 41.9999", + "| eggs | 451", + "|====", + ] + ) + result = tabulate( + _test_table, _test_table_headers, tablefmt="asciidoc", colalign=(None, "right") + ) + assert_equal(expected, result) + + def test_psql(): "Output: psql with headers" expected = "\n".join(