Skip to content

ListView.pop(index) can remove an item from a nested list instead of its own child #6804

Backlog

Description

@BichengWang

The bug

ListView.pop(index) counts direct children for its index handling, but looks up the item with self.query("ListItem")[index], which also includes nested list items. With a nested ListView in the first item, outer.pop(1) removes the inner item instead of the outer list's second item.

Reproduced on current main (5e5b7ef58b8c5572c13a3ff9027d756623661495), Textual 8.2.8, Rich 15.0.0, Python 3.14.7, macOS. I checked open and closed issues and PRs; I couldn't find a report for this nested-item deletion. Textual 8.2.8 is also the current PyPI release.

import asyncio

from textual.app import App, ComposeResult
from textual.widgets import Label, ListItem, ListView


class Demo(App):
    def compose(self) -> ComposeResult:
        yield ListView(
            ListItem(ListView(ListItem(Label("inner"), id="inner-item")), id="first"),
            ListItem(Label("second"), id="second"),
            id="outer",
        )


async def main():
    app = Demo()
    async with app.run_test() as pilot:
        outer = app.query_one("#outer", ListView)
        print("direct children before:", [child.id for child in outer.children])
        print("all items before:", [item.id for item in outer.query(ListItem)])
        await outer.pop(1)
        await pilot.pause()
        print("direct children after pop(1):", [child.id for child in outer.children])
        print("all items after:", [item.id for item in outer.query(ListItem)])


asyncio.run(main())

Actual output:

direct children before: ['first', 'second']
all items before: ['first', 'inner-item', 'second']
direct children after pop(1): ['first', 'second']
all items after: ['first', 'second']

Expected: the direct children become ['first'], and inner-item stays inside the first item's nested list. The current behavior deletes data from the wrong list while leaving the requested item in place.

Activity

  1. github-actions commented on Nov 29, 2026

    @github-actions

    We found the following entries in the FAQ which you may find helpful:

    Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

    This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

    This is an automated reply, generated by FAQtory

  2. Harp404 commented on Nov 30, 2026

    @Harp404

    I can reproduce this on main. remove_items has the same problem: it also indexes self.query("ListItem"), so remove_items([1]) on the example removes inner-item too.

    Proposed fix: index the list's direct children instead of a query, i.e. self._nodes[index] in pop and items = self._nodes in remove_items. A ListView's children are its ListItems, and the index bookkeeping in both methods already counts self._nodes, so the lookup and the index update would agree. I have this ready with two regression tests (nested ListView in the first item; pop(1), pop() and remove_items([1, -1]) remove only the outer items and keep inner-item), which fail on main and pass with the change; tests/listview passes.

    @willmcgugan, would you like a PR for this? Per the AI policy: it would be AI-assisted (Claude Code, Opus 5.5), with the template filled in.

  3. OmDhamal-08 commented on Nov 30, 2026

    @OmDhamal-08

    I can reproduce this on current main (5e5b7ef).
    @BichengWang

    remove_items() has the same problem, since it also uses self.query("ListItem"):

    python
    await outer.remove_items([1])
    # expected: outer children ['first'], inner-item kept
    # actual:   'inner-item' removed from the nested list, 'second' still there
    

    I tried this locally: pop(1), pop(), pop(-2) and remove_items([1]) all remove the correct direct child and leave the nested list untouched, and the existing tests in tests/listview pass.

    @willmcgugan would you be okay with me opening a PR with this fix and regression tests for both pop() and remove_items()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions