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

When using maxcolwidths in a column that contains None values, tabulate crashes with "NoneType takes no arguments" #312

Open gschizas opened 5 months ago

gschizas commented 5 months ago

Sample script:

from tabulate import tabulate
table = [["spam", 41.9999], ["eggs", "451.0"], ["sausages", None]]
print(tabulate(table, headers='keys', tablefmt='plain'))
print(tabulate(table, headers='keys', tablefmt='plain', maxcolwidths=40))

The last line leads to this exception:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    print(tabulate(table, headers='keys', tablefmt='plain', maxcolwidths=40))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "tabulate/__init__.py", line 2061, in tabulate
    list_of_lists = _wrap_text_to_colwidths(
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "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

(from version 0.90, in the latest commit only the line numbers change)

This is a simple fix, line 1516 (for v0.9.0) should have another guard, like so:

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

I'll do a PR soon.

fradeve commented 5 months ago

Thanks for fixing this! I have the same issue :smile:

tywallis commented 3 months ago

I am still having this issue

gschizas commented 3 months ago

I am still having this issue

Well, it hasn't been merged or published, what did you expect? 😄

RaSan147 commented 3 months ago

Thanks good sir