diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index ebc819c943..bad0298ed6 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -2132,16 +2132,13 @@ def sort_by_column_keys( result = itemgetter(*columns)(row_data) return result - _key = key - if key: - - def _key(row: tuple[RowKey, dict[ColumnKey | str, CellType]]) -> Any: - _, row_data = row - return key(itemgetter(*columns)(row_data)) + def key_wrapper(row: tuple[RowKey, dict[ColumnKey | str, CellType]]) -> Any: + _, row_data = row + return key(itemgetter(*columns)(row_data)) ordered_rows = sorted( self._data.items(), - key=_key if key is not None else sort_by_column_keys, + key=key_wrapper if key is not None else sort_by_column_keys, reverse=reverse, ) self._row_locations = TwoWayDict(