DataTable column added with the default `None` is at least four cells wide, though its cells are empty

Open Beginner friendly
#6,777 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
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

Read the DataTable methods _get_row_renderables and _update_column_widths, along with the update_cell(..., update_width=True) path mentioned in the issue. Check existing DataTable tests for column sizing and add coverage for None cells; done means their measured width matches the empty rendering and the reported table width is corrected.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets. add_column on a table that already has rows fills those rows with default, which is None. The cells are drawn empty, but the column is sized as if they contained the text None.

import asyncio

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

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

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_column("A")
        table.add_row("1")
        table.add_column("B")  # existing rows get the default, None

async def main():
    async with NewColumnApp().run_test() as pilot:
        await pilot.pause()
        table = pilot.app.query_one(DataTable)
        for column in table.ordered_columns:
            print(f"column {column.label.plain}: content_width={column.content_width}")
        print("cell B:", repr(table.get_cell_at((0, 1))))
        print("table width:", table.virtual_size.width)

asyncio.run(main())

Output on Textual 8.2.7 (and the same on main at 5e5b7ef):

column A: content_width=1
column B: content_width=4
cell B: None
table width: 9

I'd expect column B to be 1 wide (its label), and the table 6 wide.

_get_row_renderables draws a None cell as empty text, but _update_column_widths measures default_cell_formatter(None), which is str(None). update_cell(..., None, update_width=True) goes through the same path.

I'd suggest measuring None cells as empty in _update_column_widths, to match how they're drawn. I'm happy to send a PR.

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.