From 23423b6e3b413d485829619c71dff8d330a4a681 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 24 Jan 2026 09:13:56 -0500 Subject: [PATCH] pass colalign argument through to tabulate In tabulate_adapter.adapter(), permit the colalign argument to be passed in kwargs. This argument can be used by the application to set known desirable column alignments based on column types, without expensive numparse checks. In addition, this works reliably with nullable numeric columns, whereas the numparse logic does not. --- CHANGELOG | 6 ++++++ cli_helpers/tabular_output/tabulate_adapter.py | 9 ++++++++- tests/tabular_output/test_tabulate_adapter.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 0ed7bee..07f43fc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,11 @@ # Changelog +## Version 2.8.0 + +(released on 2026-01-24) + +- Pass `colalign` argument through to `tabulate`. + ## Version 2.7.0 (released on 2025-07-28) diff --git a/cli_helpers/tabular_output/tabulate_adapter.py b/cli_helpers/tabular_output/tabulate_adapter.py index f175f20..bb8f492 100644 --- a/cli_helpers/tabular_output/tabulate_adapter.py +++ b/cli_helpers/tabular_output/tabulate_adapter.py @@ -225,7 +225,14 @@ def addColorInElt(elt): def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwargs): """Wrap tabulate inside a function for TabularOutputFormatter.""" - keys = ("floatfmt", "numalign", "stralign", "showindex", "disable_numparse") + keys = ( + "floatfmt", + "numalign", + "stralign", + "showindex", + "disable_numparse", + "colalign", + ) tkwargs = {"tablefmt": table_format} tkwargs.update(filter_dict_by_key(kwargs, keys)) diff --git a/tests/tabular_output/test_tabulate_adapter.py b/tests/tabular_output/test_tabulate_adapter.py index 6e7c7db..a8ceb1f 100644 --- a/tests/tabular_output/test_tabulate_adapter.py +++ b/tests/tabular_output/test_tabulate_adapter.py @@ -42,6 +42,24 @@ def test_tabulate_wrapper(): └─────────┴────────┘""" ) + data = [["abc", 1], ["d", 456]] + headers = ["letters", "number"] + output = tabulate_adapter.adapter( + iter(data), + headers, + colalign=["left", "left"], + table_format="psql_unicode", + ) + assert "\n".join(output) == dedent( + """\ + ┌─────────┬────────┐ + │ letters │ number │ + ├─────────┼────────┤ + │ abc │ 1 │ + │ d │ 456 │ + └─────────┴────────┘""" + ) + data = [["{1,2,3}", "{{1,2},{3,4}}", "{å,魚,текст}"], ["{}", "", "{}"]] headers = ["bigint_array", "nested_numeric_array", "配列"] output = tabulate_adapter.adapter(iter(data), headers, table_format="psql")