DataTable raises `KeyError: None` when `fixed_rows` is larger than the number of rows
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Textualize/textual#6789 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Textualize/textual#6778 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day