`App.clear_notifications()` leaves an empty `ToastHolder` behind for every toast

Open
#6,788 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
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

Start in textual/widgets/_toast.py: ToastRack.show dismisses stale toasts with toast.remove(), while Toast._expire removes self.parent when it is a ToastHolder. The first thing to do is run the reproduction script from the issue and check len(rack.query(ToastHolder)) before and after app.clear_notifications(). Done means no holders remain after clearing, matching the timeout path, and the reporter's test passes. Note the reporter says a PR with a test is already prepared, so check that before starting.

Written by the indexing model from the issue text.

Description

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

ToastRack.show removes stale toasts with toast.remove(), but each Toast is wrapped in a ToastHolder, and the holder stays. Toast._expire removes the holder (self.parent if isinstance(self.parent, ToastHolder) else self), so toasts that time out are fine. Toasts removed by clear_notifications() leave their holders, and they build up in the rack.

import asyncio

import textual
from textual.app import App
from textual.widgets._toast import Toast, ToastHolder, ToastRack

async def main() -> None:
    app = App()
    async with app.run_test(notifications=True) as pilot:
        for n in range(3):
            app.notify(f"message {n}")
        await pilot.pause()
        rack = app.screen.query_one(ToastRack)
        print("after 3 notify:  holders", len(rack.query(ToastHolder)), "toasts", len(rack.query(Toast)))
        app.clear_notifications()
        await pilot.pause()
        print("after clear:     holders", len(rack.query(ToastHolder)), "toasts", len(rack.query(Toast)))
        app.notify("one more")
        await pilot.pause()
        print("after 1 notify:  holders", len(rack.query(ToastHolder)), "toasts", len(rack.query(Toast)))
    print("Textual", textual.__version__)

asyncio.run(main())

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

after 3 notify:  holders 3 toasts 3
after clear:     holders 3 toasts 0
after 1 notify:  holders 4 toasts 1

The empty holders are height: auto, so in the cases I tried nothing shows on screen. They just stay in the DOM, so an app that clears notifications often keeps adding to the rack. I expected clear_notifications() to leave no holders, as when toasts time out. Removing the holder in show the way _expire does 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.