Pressing enter in a DataTable with rows but no columns raises `CellDoesNotExist`

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

Research direction

Start in textual/widgets/_data_table.py at _post_selected_message and compare its existing no-rows early return with the no-columns case described here. Add coverage for pressing Enter with rows but no columns, including the row and column cursor types; done means no exception is raised and the empty-table behavior is preserved.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets. If a DataTable has rows but no columns yet (for example, rows added before the columns), pressing enter crashes the app.

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

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

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_row()
        table.focus()

if __name__ == "__main__":
    NoColumnsApp().run()  # then press enter

Expected: nothing happens, as with an empty table.

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

  File "textual/widgets/_data_table.py", line 2839, in action_select_cursor
    self._post_selected_message()
  File "textual/widgets/_data_table.py", line 2847, in _post_selected_message
    cell_key = self.coordinate_to_cell_key(cursor_coordinate)
  File "textual/widgets/_data_table.py", line 1291, in coordinate_to_cell_key
    raise CellDoesNotExist(f"No cell exists at {coordinate!r}.")
textual.widgets._data_table.CellDoesNotExist: No cell exists at Coordinate(row=0, column=0).

The same happens with cursor_type set to "row" or "column". _post_selected_message already returns early when there are no rows (the fix for #1970); it doesn't check for columns.

I'd suggest returning early when there are no columns as well. For the row cursor you could argue a RowSelected should still be posted, but that would need a cell key without a column, so returning early seemed the smaller change. 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.