`SelectionList.toggle()` with a value that isn't in the list selects it and then raises `KeyError`

Aperta Adatta ai principianti
#6,767 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
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend

Direzione di ricerca

Inizia in src/textual/widgets/_selection_list.py da _toggle(), poi leggi _select(), _deselect() e la ricerca di _values usata per inviare SelectionToggled; _make_selection() spiega perché _select() viene eseguito prima che _values sia valorizzato. Riproduci il problema con lo snippet nell’issue, poi fai in modo che toggle() non faccia nulla per un valore sconosciuto prima di qualsiasi mutazione o invio di messaggi, mantenendo intatto il percorso select()/_make_selection(). Esegui i test esistenti della lista di selezione in tests/ per confermare che non ci siano regressioni; il lavoro è concluso quando non si verifica KeyError, non viene generato SelectedChanged e selected rimane invariato.

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

Descrizione

Hi! I hit this while writing generated tests against Textual's widgets.

Calling SelectionList.toggle() with a value that isn't one of the list's options changes the selection, posts SelectedChanged, and only then raises KeyError. The value stays in selected, and deselect_all() doesn't remove it, since that only walks the options.

import asyncio
from textual.app import App, ComposeResult
from textual.widgets import SelectionList

class ToggleApp(App):
    def compose(self) -> ComposeResult:
        yield SelectionList(("One", 1), ("Two", 2))

    def on_selection_list_selected_changed(self, event: SelectionList.SelectedChanged) -> None:
        print("SelectedChanged, selected =", event.selection_list.selected)

async def main() -> None:
    app = ToggleApp()
    async with app.run_test() as pilot:
        selection_list = app.query_one(SelectionList)
        try:
            selection_list.toggle(3)  # 3 is not a value in the list
        except Exception as error:
            print("toggle(3) raised", type(error).__name__, error)
        await pilot.pause()
        print("selected after toggle(3):", selection_list.selected)
        selection_list.deselect_all()
        selection_list.select(4)  # also not in the list
        await pilot.pause()
        print("selected after select(4):", selection_list.selected)

asyncio.run(main())

Output (the same on 8.2.7 and on main at 5e5b7ef):

toggle(3) raised KeyError 3
SelectedChanged, selected = [3]
selected after toggle(3): [3]
SelectedChanged, selected = [3, 4]
selected after select(4): [3, 4]

_toggle() calls _select() (or _deselect()) and then looks the value up in _values to post SelectionToggled, which raises for an unknown value after the change has been made.

I'd expect toggle() of an unknown value to change nothing, the way deselect() of an unknown value does. select() of an unknown value also adds it to selected without complaint (last two lines). Is that intended? _make_selection() relies on _select() before _values is filled in, so I've left select() alone in the PR.

Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.

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.