Skip to content

Commit 0978de5

Browse files
committed
Fix separating line with dataclasses
1 parent 95ae5eb commit 0978de5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

tabulate/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,12 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
14611461
field_names = [field.name for field in dataclasses.fields(rows[0])]
14621462
if headers == "keys":
14631463
headers = field_names
1464-
rows = [[getattr(row, f) for f in field_names] for row in rows]
1464+
rows = [
1465+
[getattr(row, f) for f in field_names]
1466+
if not _is_separating_line(row)
1467+
else row
1468+
for row in rows
1469+
]
14651470

14661471
elif headers == "keys" and len(rows) > 0:
14671472
# keys are column indices

test/test_input.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test support of the various forms of tabular data."""
22

3-
from tabulate import tabulate
3+
from tabulate import tabulate, SEPARATING_LINE
44
from common import assert_equal, assert_in, raises, skip
55

66
try:
@@ -520,6 +520,28 @@ def test_py37orlater_list_of_dataclasses_headers():
520520
skip("test_py37orlater_list_of_dataclasses_headers is skipped")
521521

522522

523+
def test_py37orlater_list_of_dataclasses_with_separating_line():
524+
"Input: a list of dataclasses with a separating line"
525+
try:
526+
from dataclasses import make_dataclass
527+
528+
Person = make_dataclass("Person", ["name", "age", "height"])
529+
ld = [Person("Alice", 23, 169.5), SEPARATING_LINE, Person("Bob", 27, 175.0)]
530+
result = tabulate(ld, headers="keys")
531+
expected = "\n".join(
532+
[
533+
"name age height",
534+
"------ ----- --------",
535+
"Alice 23 169.5",
536+
"------ ----- --------",
537+
"Bob 27 175",
538+
]
539+
)
540+
assert_equal(expected, result)
541+
except ImportError:
542+
skip("test_py37orlater_list_of_dataclasses_keys is skipped")
543+
544+
523545
def test_list_bytes():
524546
"Input: a list of bytes. (issue #192)"
525547
lb = [["你好".encode("utf-8")], ["你好"]]

0 commit comments

Comments
 (0)