colalign argument crashes tabulate if no data
This crashes tabulate:
In [2]: tabulate.tabulate([], headers=['a', 'b', 'c', 'd', 'e'], colalign=['left', 'left', 'left', 'left', 'left'])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[2], line 1
----> 1 tabulate.tabulate([], headers=['a', 'b', 'c', 'd', 'e'], colalign=['left', 'left', 'left', 'left', 'left'])
File /opt/homebrew/Caskroom/mambaforge/base/envs/work/lib/python3.11/site-packages/tabulate/__init__.py:2165, in tabulate(tabular_data, headers, tablefmt, floatfmt, intfmt, numalign, stralign, missingval, showindex, disable_numparse, colalign, maxcolwidths, rowalign, maxheadercolwidths)
2163 assert isinstance(colalign, Iterable)
2164 for idx, align in enumerate(colalign):
-> 2165 aligns[idx] = align
2166 minwidths = (
2167 [width_fn(h) + min_padding for h in headers] if headers else [0] * len(cols)
2168 )
2169 cols = [
2170 _align_column(c, a, minw, has_invisible, enable_widechars, is_multiline)
2171 for c, a, minw in zip(cols, aligns, minwidths)
2172 ]
IndexError: list assignment index out of range
Removing the colalign argument resolves the crash, and tabulate outputs correctly an empty table. Adding data into the table also resolves the crash, and tabulate outputs correctly a table with that data.
Versions: tabulate 0.9.0 python 3.11.5 mamba 1.5.1 conda 23.7.4 macos 13.5.2
The problem is fixed with #221, but not yet released or pushed on PyPi. You'll need to install from source for now:
git clone https://github.com/astanin/python-tabulate.git
pip install ./python-tabulate
python
Tested in Python 3.11.5
>>> import tabulate
>>> print(tabulate.tabulate([], headers=['a', 'b', 'c', 'd', 'e'], colalign=['left', 'left', 'left', 'left', 'left']))
a b c d e
--- --- --- --- ---
Also, notice that #221 made your life simpler with the colglobalalign keyword. Indeed:
>>> import tabulate
>>> print(tabulate.tabulate([], headers=['a', 'b', 'c', 'd', 'e'], colglobalalign='left'))
a b c d e
--- --- --- --- ---
Furthermore, you can now align headers differently from data (see help(tabulate.tabulate)).
Cheers!
Outstanding work!