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.17k stars 164 forks source link

Fix #124. Improve error reporting when non-iterable data provided. #233

Closed ilya112358 closed 1 month ago

ilya112358 commented 1 year ago

Fix #124. Also related to #208 and #191.

This is to provide an additional message to the user when the program throws a ... is not iterable exception.

Two blocks of try-except added: one for the case when a non-iterable is passed and another when an iterable of non-iterables is passed.

Typical output:

>>> print(tabulate({"a": 1, "b": 2}))
Traceback (most recent call last):
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 1373, in _normalize_tabular_data
    izip_longest(*tabular_data.values())
TypeError: 'int' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 2080, in tabulate
    list_of_lists, headers = _normalize_tabular_data(
                             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 1376, in _normalize_tabular_data
    raise TypeError(err_msg)
TypeError:

To build a table python-tabulate requires two-dimensional data like a list of lists or similar.
Did you forget a pair of extra [] or ',' in ()?
>>>