From 2ed72e17107a212626d7b26e93704f5a543d1fef Mon Sep 17 00:00:00 2001 From: darrenburns Date: Tue, 7 Mar 2023 20:58:28 +0000 Subject: [PATCH] DataTable - fix crash when selection made in empty table (#1973) --- CHANGELOG.md | 1 + src/textual/widgets/_data_table.py | 2 ++ tests/test_data_table.py | 9 +++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9379b4f5..a008e826f 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 diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 15a51052c..649e495e4 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -1975,6 +1975,8 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): """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 9c578ac3d..c906754ee 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():