Skip to content

Commit

Permalink
Fix DataTable crash when removing and updating cell at the same time (
Browse files Browse the repository at this point in the history
#3487)

* Fix crash when removing rows/columns from datatable

* Regression tests for datatable crash

* Update CHANGELOG regarding datatable crash fix
  • Loading branch information
darrenburns authored Oct 9, 2023
1 parent 0ffa82a commit 4054667
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix location of IME and emoji popups https://github.com/Textualize/textual/pull/3408
- Fixed application freeze when pasting an emoji into an application on Windows https://github.com/Textualize/textual/issues/3178
- Fixed duplicate option ID handling in the `OptionList` https://github.com/Textualize/textual/issues/3455
- Fix crash when removing and updating DataTable cell at same time https://github.com/Textualize/textual/pull/3487
- Fixed fractional styles to allow integer values https://github.com/Textualize/textual/issues/3414

### Added
Expand Down
10 changes: 8 additions & 2 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,10 @@ def remove_row(self, row_key: RowKey | str) -> None:

self._row_locations = new_row_locations

# Prevent the removed cells from triggering dimension updates
for column_key in self._data.get(row_key):
self._updated_cells.discard(CellKey(row_key, column_key))

del self.rows[row_key]
del self._data[row_key]

Expand Down Expand Up @@ -1668,8 +1672,10 @@ def remove_column(self, column_key: ColumnKey | str) -> None:
self._column_locations = new_column_locations

del self.columns[column_key]
for row in self._data:
del self._data[row][column_key]

for row_key in self._data:
self._updated_cells.discard(CellKey(row_key, column_key))
del self._data[row_key][column_key]

self.cursor_coordinate = self.cursor_coordinate
self.hover_coordinate = self.hover_coordinate
Expand Down
26 changes: 26 additions & 0 deletions tests/test_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ async def test_remove_row():
assert len(table.rows) == 2


async def test_remove_row_and_update():
"""Regression test for https://github.com/Textualize/textual/issues/3470 -
Crash when attempting to remove and update the same cell."""
app = DataTableApp()
async with app.run_test() as pilot:
table: DataTable = app.query_one(DataTable)
table.add_column("A", key="A")
table.add_row("1", key="1")
table.update_cell("1", "A", "X", update_width=True)
table.remove_row("1")
await pilot.pause()


async def test_remove_column():
app = DataTableApp()
async with app.run_test():
Expand All @@ -323,6 +336,19 @@ async def test_remove_column():
assert table.get_row_at(2) == ["2/1"]


async def test_remove_column_and_update():
"""Regression test for https://github.com/Textualize/textual/issues/3470 -
Crash when attempting to remove and update the same cell."""
app = DataTableApp()
async with app.run_test() as pilot:
table: DataTable = app.query_one(DataTable)
table.add_column("A", key="A")
table.add_row("1", key="1")
table.update_cell("1", "A", "X", update_width=True)
table.remove_column("A")
await pilot.pause()


async def test_clear():
app = DataTableApp()
async with app.run_test():
Expand Down

0 comments on commit 4054667

Please sign in to comment.