DataTable raises `KeyError: None` when `fixed_rows` is larger than the number of rows

Aperta Adatta ai principianti
#6,762 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Il crash ha origine in render_line in textual/widgets/_data_table.py, che costruisce le chiavi delle righe fisse a partire da self._row_locations.get_key(row_index); questo restituisce None oltre l’ultima riga, e quindi get_row_height non riesce ad accedere a self.rows[None]. Confrontalo con _get_fixed_offset, che usa già ordered_rows[: self.fixed_rows], e riproduci il problema con lo snippet nell’issue (una DataTable vuota con fixed_rows=1). Il lavoro è completo quando i casi di tabella vuota e corta mostrano l’intestazione senza un KeyError e un test di regressione copre il caso in cui fixed_rows è maggiore del numero di righe.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi! I hit this while writing generated tests against Textual's widgets. A DataTable with fixed_rows set crashes on its first render if it has fewer rows than that, which includes the common case of an empty table whose rows are loaded later.

from textual.app import App, ComposeResult
from textual.widgets import DataTable

class FixedRowsApp(App):
    def compose(self) -> ComposeResult:
        yield DataTable(fixed_rows=1)

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_columns("A", "B")
        # Rows will be loaded later.

if __name__ == "__main__":
    FixedRowsApp().run()

Expected: an empty table with its header.

Actual (on Textual 8.2.7, and main at 5e5b7ef):

  File "textual/widgets/_data_table.py", line 2487, in render_line
    fixed_rows_height = sum(
        self.get_row_height(row_key) for row_key in fixed_row_keys
    )
  File "textual/widgets/_data_table.py", line 2488, in <genexpr>
    self.get_row_height(row_key) for row_key in fixed_row_keys
  File "textual/widgets/_data_table.py", line 1121, in get_row_height
    return self.rows[row_key].height
KeyError: None

render_line builds the fixed row keys with self._row_locations.get_key(row_index) for row_index in range(self.fixed_rows), and get_key returns None for an index past the last row. The same happens with fixed_rows=3 and one row. _get_fixed_offset already slices ordered_rows[: self.fixed_rows], so it copes; only render_line doesn't.

I'd suggest clamping to the row count in render_line. I'm happy to send a PR with a test.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.