`ListView.remove_items()` doesn't adjust the highlighted index when `indices` is a generator

Open
#6,770 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
50/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.