diff --git a/src/textual/message_pump.py b/src/textual/message_pump.py index 648b0a4d7..ba7cc3485 100644 --- a/src/textual/message_pump.py +++ b/src/textual/message_pump.py @@ -125,8 +125,17 @@ class MessagePump(metaclass=MessagePumpMeta): @property def is_attached(self) -> bool: - """Check the node is attached to the DOM""" - return self._parent is not None + """Check the node is attached to the app via the DOM.""" + + from .app import App + + node = self + + while not isinstance(node, App): + if node._parent is None: + return False + node = node._parent + return True def _attach(self, parent: MessagePump) -> None: """Set the parent, and therefore attach this node to the tree. diff --git a/tests/test_widget_removing.py b/tests/test_widget_removing.py index e050bb09d..a33860c83 100644 --- a/tests/test_widget_removing.py +++ b/tests/test_widget_removing.py @@ -8,9 +8,13 @@ from textual.containers import Container async def test_remove_single_widget(): """It should be possible to the only widget on a screen.""" async with App().run_test() as pilot: - await pilot.app.mount(Static()) + widget = Static() + assert not widget.is_attached + await pilot.app.mount(widget) + assert widget.is_attached assert len(pilot.app.screen.children) == 1 await pilot.app.query_one(Static).remove() + assert not widget.is_attached assert len(pilot.app.screen.children) == 0