Skip to content

fix: prevent KeyError when clearing and re-rendering DataTable#6591

Open
xbrxr03 wants to merge 1 commit into
Textualize:mainfrom
xbrxr03:fix/datatable-keyerror-on-clear
Open

fix: prevent KeyError when clearing and re-rendering DataTable#6591
xbrxr03 wants to merge 1 commit into
Textualize:mainfrom
xbrxr03:fix/datatable-keyerror-on-clear

Conversation

@xbrxr03

@xbrxr03 xbrxr03 commented Jun 22, 2026

Copy link
Copy Markdown

Summary

Fixes #4900

DataTable.clear() was not clearing _updated_cells and _new_rows, leaving stale RowKey references that could cause a KeyError when the table was re-rendered after clearing and adding new rows.

Root Cause

When DataTable.clear() is called, it clears self.rows, self._data, and self._row_locations, but it does not clear self._updated_cells or self._new_rows. These sets retain stale RowKey/CellKey objects that reference rows which no longer exist in the table.

Since RowKey(value=None) uses id(self) as its hash, these stale key objects will never match any key in the rebuilt self.rows dict. When the on_idle handler processes these stale references, it can trigger a KeyError in code paths that access self.rows[row_key] directly (without .get()).

Notably, remove_row() already cleans up _updated_cells for the removed row — clear() should do the same for all rows.

Fix

Add two lines to DataTable.clear() to clear the stale reference sets:

self._updated_cells.clear()
self._new_rows.clear()

Test

Added test_clear_clears_updated_cells_and_new_rows which verifies that both _updated_cells and _new_rows are empty after calling clear().

Checklist

  • Fix is minimal — only adds 2 lines
  • Tests pass (88/88)
  • Regression test added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError error occurred in DataTable

2 participants