DataTable: rows rendered before column widths are measured stay truncated (row/line caches not invalidated in _update_dimensions)

Open Beginner friendly
#6,759 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend, testing

Research direction

Start by reading DataTable._update_dimensions and its cache handling, then compare it with _on_idle and the reproduction in the issue. Reproduce the mixed row widths by rendering rows before the idle handler runs; done means every row renders with the measured column widths afterward.

Written by the indexing model from the issue text.

Description

Description

When a DataTable is rebuilt (clear(columns=True), add_column, add_row) and the screen repaints before the table's idle handler has measured the new rows, the header and the rows painted in that window are rendered with the label-only column widths – and they stay that way after the widths have been computed. Rows painted later use the correct widths, so the table ends up with a mix of truncated and full rows.

I ran into this in task-tui, which rebuilds its table on every report switch and after leaving edit mode (the focus/class change triggers the repaint). Resizing the terminal fixes the display.

Minimal reproduction

import asyncio

from textual.app import App, ComposeResult
from textual.widgets import DataTable

ROWS = [(str(i), "a-rather-long-project-name", "20261113", "some description") for i in range(5)]


class TableApp(App):
    def compose(self) -> ComposeResult:
        yield DataTable()

    def rebuild(self) -> None:
        table = self.query_one(DataTable)
        table.clear(columns=True)
        for label in ("ID", "Project", "Due", "Description"):
            table.add_column(label, key=label)
        for i, row in enumerate(ROWS):
            table.add_row(*row, key=f"row{i}")


async def main() -> None:
    app = TableApp()
    async with app.run_test(size=(80, 10)) as pilot:
        table = app.query_one(DataTable)
        app.rebuild()  # first build, settles normally
        await pilot.pause()
        app.rebuild()  # rebuild the same table (e.g. after a filter change)
        # A repaint before the DataTable's idle handler has measured the new rows
        # (in a real app: focus change, class change, any screen refresh):
        for y in range(3):
            table.render_line(y)
        await pilot.pause()  # idle: column widths are now computed
        for y in range(6):
            print(table.render_line(y).text.rstrip())


asyncio.run(main())
Actual output
 ID  Project  Due  Description
 0   a-rathe  202  some descri
 1   a-rathe  202  some descri
 2   a-rather-long-project-name  20261113  some description
 3   a-rather-long-project-name  20261113  some description
 4   a-rather-long-project-name  20261113  some description
Expected output

All lines rendered with the measured column widths:

 ID  Project                     Due       Description
 0   a-rather-long-project-name  20261113  some description
 ...

Analysis

add_row only records the row in _new_rows; column widths are measured later in _on_idle → _update_dimensions, which updates column.content_width. Rendered rows and lines are cached in _row_render_cache / _line_cache with keys that include _update_count. _update_dimensions changes the widths but neither bumps _update_count nor clears these caches (_line_cache is only cleared in the auto_height_rows branch), so lines cached during the window between add_row and idle are served with the old widths.

Suggested fix

Invalidate the render caches once the widths have been recomputed, e.g. at the end of _update_dimensions:

        self._clear_caches()

(or self._update_count += 1). With this one line the reproduction above renders correctly.

Environment

  • Textual 8.2.6 (also reproduced with 8.2.8, the current release)
  • Rich 14.x, Python 3.13
  • NixOS 26.05, KDE Konsole/Yakuake
Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.