Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix separating line with dataclasses #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,12 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
field_names = [field.name for field in dataclasses.fields(rows[0])]
if headers == "keys":
headers = field_names
rows = [[getattr(row, f) for f in field_names] for row in rows]
rows = [
[getattr(row, f) for f in field_names]
if not _is_separating_line(row)
else row
for row in rows
]

elif headers == "keys" and len(rows) > 0:
# keys are column indices
Expand Down
24 changes: 23 additions & 1 deletion test/test_input.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test support of the various forms of tabular data."""

from tabulate import tabulate
from tabulate import tabulate, SEPARATING_LINE
from common import assert_equal, assert_in, raises, skip

try:
Expand Down Expand Up @@ -520,6 +520,28 @@ def test_py37orlater_list_of_dataclasses_headers():
skip("test_py37orlater_list_of_dataclasses_headers is skipped")


def test_py37orlater_list_of_dataclasses_with_separating_line():
"Input: a list of dataclasses with a separating line"
try:
from dataclasses import make_dataclass

Person = make_dataclass("Person", ["name", "age", "height"])
ld = [Person("Alice", 23, 169.5), SEPARATING_LINE, Person("Bob", 27, 175.0)]
result = tabulate(ld, headers="keys")
expected = "\n".join(
[
"name age height",
"------ ----- --------",
"Alice 23 169.5",
"------ ----- --------",
"Bob 27 175",
]
)
assert_equal(expected, result)
except ImportError:
skip("test_py37orlater_list_of_dataclasses_keys is skipped")


def test_list_bytes():
"Input: a list of bytes. (issue #192)"
lb = [["你好".encode("utf-8")], ["你好"]]
Expand Down