astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.08k stars 162 forks source link

TypeError when cell is None #323

Open viewercq opened 4 months ago

viewercq commented 4 months ago

https://github.com/astanin/python-tabulate/blob/95ae5eb61ef969749e904c90ab429003238d6212/tabulate/__init__.py#L1532C27-L1532C61

   File "C:\Python38\lib\site-packages\tabulate\__init__.py", line 2061, in tabulate
    list_of_lists = _wrap_text_to_colwidths(
  File "C:\Python38\lib\site-packages\tabulate\__init__.py", line 1516, in _wrap_text_to_colwidths
    str(cell) if _isnumber(cell) else _type(cell, numparse)(cell)
TypeError: NoneType takes no arguments
>>> type(None)(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: NoneType takes no arguments

str(cell) if _isnumber(cell) else _type(cell, numparse)(cell) to str(cell or '') if _isnumber(cell) or cell is None else _type(cell, numparse)(cell)

bragov4ik commented 3 months ago

Seems very related: I get "list index out of range" when there are no elements to tabulate and maxcolwidths is set (e.g. tabulate([],maxcolwidths=[2])):

File ....venv/lib/python3.12/site-packages/tabulate/__init__.py:2054, in tabulate(tabular_data, headers, tablefmt, floatfmt, intfmt, numalign, stralign, missingval, showindex, disable_numparse, colalign, maxcolwidths, rowalign, maxheadercolwidths)
   2051 list_of_lists, separating_lines = _remove_separating_lines(list_of_lists)
   2053 if maxcolwidths is not None:
-> 2054     num_cols = len(list_of_lists[0])
   2055     if isinstance(maxcolwidths, int):  # Expand scalar for all columns
   2056         maxcolwidths = _expand_iterable(maxcolwidths, num_cols, maxcolwidths)

Writing here since basically the same code area/workflow