Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datatable): raise error for update_cell with invalid column key #3336

Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed `DataTable.update_cell` not raising an error with an invalid column key https://github.com/Textualize/textual/issues/3335

## [0.38.1] - 2023-09-21

### Fixed
Expand Down
11 changes: 7 additions & 4 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,12 +745,15 @@ def update_cell(
if isinstance(column_key, str):
column_key = ColumnKey(column_key)

try:
self._data[row_key][column_key] = value
except KeyError:
if (
row_key not in self._row_locations
or column_key not in self._column_locations
):
raise CellDoesNotExist(
f"No cell exists for row_key={row_key!r}, column_key={column_key!r}."
) from None
)

self._data[row_key][column_key] = value
self._update_count += 1

# Recalculate widths if necessary
Expand Down
11 changes: 11 additions & 0 deletions tests/test_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,17 @@ async def test_update_cell_cell_doesnt_exist():
table.update_cell("INVALID", "CELL", "Value")


async def test_update_cell_invalid_column_key():
"""Regression test for https://github.com/Textualize/textual/issues/3335"""
app = DataTableApp()
async with app.run_test():
table = app.query_one(DataTable)
table.add_column("Column1", key="C1")
table.add_row("TargetValue", key="R1")
with pytest.raises(CellDoesNotExist):
table.update_cell("R1", "INVALID_COLUMN", "New Value")


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