prettytable icon indicating copy to clipboard operation
prettytable copied to clipboard

Align Issue with add_autoindex

Open myheroyuki opened this issue 2 years ago • 1 comments

When trying to fix issues with header_align I noticed an existing issue with how the alignment is set in add_autoindex. The alignment for the index column is set based on the existing values of self.align and self.valign. This would be fine if those values were strings, but they can be dicts as well. In this case the align[<index_fieldname>] value gets set to a dict, creating an issue with embedded dictionaries.

What did you do?

I created a table with align values and then added an auto_index column.

What did you expect to happen?

I expected the index column to be properly aligned.

What actually happened?

The index column was not properly aligned when calling get_string. When calling get_html_string the program crashed with an TypeError: unhashable type: 'dict' error.

Please include code that reproduces the issue.

import prettytable
from prettytable import PrettyTable


def helper_table(rows=3):
    t = PrettyTable(["Field 1", "Field 2", "Field 3"])
    v = 1
    for row in range(rows):
        # Some have spaces, some not, to help test padding columns of different widths
        t.add_row([f"value {v*100}", f"value {v+1*100}", f"value {v+2*100}"])
        v += 3
    return t



t = helper_table(rows=3)
t.format = True
t.align = "r"
t.align["Field 1"] = "l"
t.valign["Field 1"] = "T"
t.add_autoindex("I")

print(t.get_string())
print(t.get_html_string())

The output of running this is shown below.

+------+-----------+-----------+-----------+
|  I   | Field 1   |   Field 2 |   Field 3 |
+------+-----------+-----------+-----------+
| 1000 | value 100 | value 101 | value 201 |
| 1001 | value 400 | value 104 | value 204 |
| 1002 | value 700 | value 107 | value 207 |
+------+-----------+-----------+-----------+
Traceback (most recent call last):
  File "tests/testy.py", line 24, in <module>
    print(t.get_html_string())
  File "/home/user/repo/prettytable/venv/lib/python3.8/site-packages/prettytable-3.2.1.dev44-py3.8.egg/prettytable/prettytable.py", line 2079, in get_html_string
  File "/home/user/repo/prettytable/venv/lib/python3.8/site-packages/prettytable-3.2.1.dev44-py3.8.egg/prettytable/prettytable.py", line 2199, in _get_formatted_html_string
TypeError: unhashable type: 'dict'

myheroyuki avatar Sep 21 '22 13:09 myheroyuki

@hugovk I noticed in the original add_autoindex pull request (#106) you pushed for automatically using the existing alignment values instead of having the user be able to set them specifically in this function. Do you still agree with that?

Either way, this could be solved by simply checking the type of self.align before setting the new alignment to be the same, which is probably the best short-term solution.

On the other hand, I've noticed alignment seems to cause a fair number of issues in this project, and the way it is implemented is not the most intuitive. Perhaps it would be better to refactor the way alignment works internally? This could be done as part of the work needed to add the separate header_align attribute from issue #102

myheroyuki avatar Sep 21 '22 13:09 myheroyuki