`ListView.remove_items()` doesn't adjust the highlighted index when `indices` is a generator
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 50/100
Direzione di ricerca
The bug is in ListView.remove_items(), which iterates the indices argument twice: once to pick the items to remove and again to build normalized_indices used to adjust the highlighted index. Run the reproduction script with a list and with a generator to confirm the mismatch. Done means both inputs leave index and highlighted_child identical. The reporter says a PR with a test exists but is not linked, so check the thread before starting.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hi! I hit this while writing generated tests against Textual's widgets.
remove_items(indices: Iterable[int]) iterates indices twice: once to find the items to remove, and again to build normalized_indices. With a generator the second pass is empty, so the index isn't adjusted for the removed items.
import asyncio
import textual
from textual.app import App, ComposeResult
from textual.widgets import Label, ListItem, ListView
class ListApp(App):
def compose(self) -> ComposeResult:
yield ListView(*[ListItem(Label(f"item {n}"), id=f"item{n}") for n in range(5)])
async def run(label: str, make_indices) -> None:
app = ListApp()
async with app.run_test() as pilot:
list_view = app.query_one(ListView)
list_view.index = 3
await pilot.pause()
await list_view.remove_items(make_indices())
await pilot.pause()
highlighted = list_view.highlighted_child
print(
f"{label:>9}: index={list_view.index}",
f"highlighted={highlighted.id if highlighted else None}",
)
async def main() -> None:
print("highlight item3, then remove items 0 and 1")
await run("list", lambda: [0, 1])
await run("generator", lambda: (n for n in [0, 1]))
print("Textual", textual.__version__)
asyncio.run(main())
Output on Textual 8.2.7 (and the same on main at 5e5b7ef):
highlight item3, then remove items 0 and 1
list: index=1 highlighted=item3
generator: index=3 highlighted=None
With a generator, index stays at 3, which is now past the end of the three remaining items, so nothing is highlighted. I expected the same result as with a list. Turning indices into a list at the top of the method fixes it; I have a PR with a test ready.
- Lingua principale
- Python
- Stelle
- 37.7k
- Fork
- 1.5k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Textualize/textual
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Textualize/textual#6790 · 2 commenti ·
-
Markdown table of contents: the node added for a skipped heading level shows the wrong numeralAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
Textualize/textual#6789 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Textualize/textual#6787 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Textualize/textual#6782 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
Textualize/textual#6778 · 1 commento ·
Tutte le issue di Textualize/textual
Issue simili
-
author-page correction move-papers
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
acl-org/acl-anthology#10978 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
PostgreSQL results table stores labels with single quotes, and a double quote in a label fails the writeForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
I maintainer di solito rispondono entro 4 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
HKUDS/CLI-Anything#652 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Project-N-E-K-O/N.E.K.O#3813 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Dao-AILab/flash-attention#3191 ·
I maintainer di solito rispondono entro 4 giorni