diff --git a/news/48.bugfix.md b/news/48.bugfix.md new file mode 100644 index 00000000..76fb1984 --- /dev/null +++ b/news/48.bugfix.md @@ -0,0 +1 @@ +Converted non-string table cells to strings during rendering. diff --git a/src/cleo/ui/table.py b/src/cleo/ui/table.py index 7865b34a..e7b8c8a9 100644 --- a/src/cleo/ui/table.py +++ b/src/cleo/ui/table.py @@ -421,6 +421,10 @@ def _build_table_rows(self, rows: Rows) -> Iterator[Row | TableSeparator]: # Remove any new line breaks and replace it with a new line for column, cell in enumerate(rows[row_key]): + if not isinstance(cell, str): + cell = str(cell) + rows[row_key][column] = cell + colspan = cell.colspan if isinstance(cell, TableCell) else 1 if column in self._column_max_widths and self._column_max_widths[ diff --git a/tests/ui/test_table.py b/tests/ui/test_table.py index d10b60c2..c250b390 100644 --- a/tests/ui/test_table.py +++ b/tests/ui/test_table.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import TYPE_CHECKING +from typing import cast import pytest @@ -428,6 +429,24 @@ def test_render( assert io.fetch_output() == expected +def test_render_non_string_cells(io: BufferedIO) -> None: + table = Table(io) + table.set_headers(["Value", "Detail"]) + table.set_rows(cast("Rows", [[None, 12], [False, "ready"]])) + + table.render() + + expected = """\ ++-------+--------+ +| Value | Detail | ++-------+--------+ +| None | 12 | +| False | ready | ++-------+--------+ +""" + assert io.fetch_output() == expected + + def test_column_style(io: BufferedIO) -> None: table = Table(io) table.set_headers(["ISBN", "Title", "Author", "Price"])