From 51a8a264fde0042e087376d26b0aec237ab74fb6 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 7 Mar 2023 19:48:47 +0000 Subject: [PATCH 1/2] Testing empty DataTable interactions --- src/textual/widgets/_data_table.py | 2 ++ tests/test_data_table.py | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 15a51052c2..649e495e4b 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -1975,6 +1975,8 @@ def _post_selected_message(self): """Post the appropriate message for a selection based on the `cursor_type`.""" cursor_coordinate = self.cursor_coordinate cursor_type = self.cursor_type + if len(self._data) == 0: + return cell_key = self.coordinate_to_cell_key(cursor_coordinate) if cursor_type == "cell": self.post_message( diff --git a/tests/test_data_table.py b/tests/test_data_table.py index 9c578ac3d4..c906754eed 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -1,14 +1,12 @@ from __future__ import annotations import pytest -from rich.style import Style from rich.text import Text from textual._wait import wait_for_idle from textual.actions import SkipAction from textual.app import App from textual.coordinate import Coordinate -from textual.events import Click, MouseMove from textual.geometry import Offset from textual.message import Message from textual.widgets import DataTable @@ -166,6 +164,13 @@ async def test_datatable_message_emission(): assert app.message_names == expected_messages +async def test_empty_table_interactions(): + app = DataTableApp() + async with app.run_test() as pilot: + await pilot.press("enter", "up", "down", "left", "right") + assert app.message_names == [] + + async def test_add_rows(): app = DataTableApp() async with app.run_test(): From 38fe9a364b800f3372f37d4cf46b265122c173c3 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 7 Mar 2023 19:49:53 +0000 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9379b4f55..a008e826f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed bug that prevented pilot from pressing some keys https://github.com/Textualize/textual/issues/1815 - DataTable race condition that caused crash https://github.com/Textualize/textual/pull/1962 +- DataTable crash when enter pressed when table is empty https://github.com/Textualize/textual/pull/1973 ## [0.13.0] - 2023-03-02