From 4a5feba2346b294047222d8db3e7a483f4942cb9 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Tue, 2 Jan 2024 21:19:18 -0600 Subject: [PATCH 1/2] empty aligns no longer gives a KeyError --- tabulate/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 11bb865..0b0d99b 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2207,7 +2207,7 @@ def tabulate( else: # default aligns = [numalign if ct in [int, float] else stralign for ct in coltypes] # then specific alignements - if colalign is not None: + if colalign is not None and aligns: assert isinstance(colalign, Iterable) if isinstance(colalign, str): warnings.warn(f"As a string, `colalign` is interpreted as {[c for c in colalign]}. Did you mean `colglobalalign = \"{colalign}\"` or `colalign = (\"{colalign}\",)`?", stacklevel=2) From 216a36ea30c37018641f54cc110579bc944c7574 Mon Sep 17 00:00:00 2001 From: Andrew Coffey Date: Tue, 2 Jan 2024 21:33:03 -0600 Subject: [PATCH 2/2] added regression test --- test/test_regression.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_regression.py b/test/test_regression.py index 8f60ce7..d2fc3a0 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -512,3 +512,14 @@ def test_numpy_int64_as_integer(): assert_equal(expected, result) except ImportError: raise skip("") + +def test_empty_table_with_colalign(): + "Regression: empty table with colalign kwarg" + table = tabulate([], ["a", "b", "c"], colalign=("center", "left", "left", "center")) + expected = "\n".join( + [ + "a b c", + "--- --- ---", + ] + ) + assert_equal(expected, table)