DataTable raises `KeyError: None` when `fixed_rows` is larger than the number of rows

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

Research direction

The crash comes from render_line in textual/widgets/_data_table.py, which builds fixed row keys from self._row_locations.get_key(row_index); that returns None past the last row, and get_row_height then fails on self.rows[None]. Compare with _get_fixed_offset, which already slices ordered_rows[: self.fixed_rows], and reproduce with the snippet in the issue (an empty DataTable with fixed_rows=1). Done when the empty and short-table cases render the header without a KeyError and a regression test covers fixed_rows larger than the row count.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets. A DataTable with fixed_rows set crashes on its first render if it has fewer rows than that, which includes the common case of an empty table whose rows are loaded later.

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

class FixedRowsApp(App):
    def compose(self) -> ComposeResult:
        yield DataTable(fixed_rows=1)

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_columns("A", "B")
        # Rows will be loaded later.

if __name__ == "__main__":
    FixedRowsApp().run()

Expected: an empty table with its header.

Actual (on Textual 8.2.7, and main at 5e5b7ef):

  File "textual/widgets/_data_table.py", line 2487, in render_line
    fixed_rows_height = sum(
        self.get_row_height(row_key) for row_key in fixed_row_keys
    )
  File "textual/widgets/_data_table.py", line 2488, in <genexpr>
    self.get_row_height(row_key) for row_key in fixed_row_keys
  File "textual/widgets/_data_table.py", line 1121, in get_row_height
    return self.rows[row_key].height
KeyError: None

render_line builds the fixed row keys with self._row_locations.get_key(row_index) for row_index in range(self.fixed_rows), and get_key returns None for an index past the last row. The same happens with fixed_rows=3 and one row. _get_fixed_offset already slices ordered_rows[: self.fixed_rows], so it copes; only render_line doesn't.

I'd suggest clamping to the row count in render_line. I'm happy to send a PR with a test.

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.