You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importprettytablefromprettytableimportPrettyTabledefhelper_table(rows=3):
t=PrettyTable(["Field 1", "Field 2", "Field 3"])
v=1forrowinrange(rows):
# Some have spaces, some not, to help test padding columns of different widthst.add_row([f"value {v*100}", f"value {v+1*100}", f"value {v+2*100}"])
v+=3returntt=helper_table(rows=3)
t.format=Truet.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'
The text was updated successfully, but these errors were encountered:
@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
When trying to fix issues with
header_align
I noticed an existing issue with how the alignment is set inadd_autoindex
. The alignment for the index column is set based on the existing values ofself.align
andself.valign
. This would be fine if those values were strings, but they can be dicts as well. In this case thealign[<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 callingget_html_string
the program crashed with anTypeError: unhashable type: 'dict'
error.Please include code that reproduces the issue.
The output of running this is shown below.
The text was updated successfully, but these errors were encountered: