python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

TypeError when cell is None

Open viewercq opened this issue 1 year ago • 3 comments

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

  • exception traceback:
   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
  • reason:
>>> type(None)(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: NoneType takes no arguments
  • solution:

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)

viewercq avatar Apr 18 '24 07:04 viewercq

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

bragov4ik avatar May 05 '24 22:05 bragov4ik