Skip to content

Commit 0324fb9

Browse files
committed
Hoist WidgetError and MountError to the top level of widget.py
1 parent fbbd8a2 commit 0324fb9

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/textual/widget.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class RenderCache(NamedTuple):
147147
lines: Lines
148148

149149

150+
class WidgetError(Exception):
151+
"""Base widget error."""
152+
153+
154+
class MountError(WidgetError):
155+
"""Error raised when there was a problem with the mount request."""
156+
157+
150158
@rich.repr.auto
151159
class Widget(DOMNode):
152160
"""
@@ -189,9 +197,6 @@ class Widget(DOMNode):
189197
hover_style: Reactive[Style] = Reactive(Style, repaint=False)
190198
highlight_link_id: Reactive[str] = Reactive("")
191199

192-
class WidgetError(Exception):
193-
"""Base widget error."""
194-
195200
def __init__(
196201
self,
197202
*children: Widget,
@@ -238,7 +243,7 @@ def __init__(
238243
)
239244

240245
if self in children:
241-
raise self.WidgetError("A widget can't be its own parent")
246+
raise WidgetError("A widget can't be its own parent")
242247

243248
self._add_children(*children)
244249

@@ -381,9 +386,6 @@ def _get_virtual_dom(self) -> Iterable[Widget]:
381386
if self._scrollbar_corner is not None:
382387
yield self._scrollbar_corner
383388

384-
class MountError(WidgetError):
385-
"""Error raised when there was a problem with the mount request."""
386-
387389
def _find_mount_point(self, spot: int | str | "Widget") -> tuple["Widget", int]:
388390
"""Attempt to locate the point where the caller wants to mount something.
389391
@@ -394,7 +396,7 @@ def _find_mount_point(self, spot: int | str | "Widget") -> tuple["Widget", int]:
394396
tuple[Widget, int]: The parent and the location in its child list.
395397
396398
Raises:
397-
Widget.MountError: If there was an error finding where to mount a widget.
399+
MountError: If there was an error finding where to mount a widget.
398400
399401
The rules of this method are:
400402
@@ -421,7 +423,7 @@ def _find_mount_point(self, spot: int | str | "Widget") -> tuple["Widget", int]:
421423
# have a parent? There's no way we can use it as a sibling to make
422424
# mounting decisions if it doesn't have a parent.
423425
if spot.parent is None:
424-
raise self.MountError(
426+
raise MountError(
425427
f"Unable to find relative location of {spot!r} because it has no parent"
426428
)
427429

@@ -431,7 +433,7 @@ def _find_mount_point(self, spot: int | str | "Widget") -> tuple["Widget", int]:
431433
try:
432434
return spot.parent, spot.parent.children.index(spot)
433435
except ValueError:
434-
raise self.MountError(f"{spot!r} is not a child of {self!r}") from None
436+
raise MountError(f"{spot!r} is not a child of {self!r}") from None
435437

436438
def mount(
437439
self,
@@ -459,7 +461,7 @@ def mount(
459461

460462
# Saying you want to mount before *and* after something is an error.
461463
if before is not None and after is not None:
462-
raise self.MountError(
464+
raise MountError(
463465
"Only one of `before` or `after` can be handled -- not both"
464466
)
465467

tests/test_widget_mount_point.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from textual.widget import Widget
3+
from textual.widget import Widget, MountError
44

55

66
class Content(Widget):
@@ -36,5 +36,5 @@ def test_find_dom_spot():
3636
# Finally, let's be sure that we get an error if, for some odd reason,
3737
# we go looking for a widget that isn't actually part of the DOM we're
3838
# looking in.
39-
with pytest.raises(Widget.MountError):
39+
with pytest.raises(MountError):
4040
_ = screen._find_mount_point(Widget())

tests/test_widget_mounting.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from textual.app import App
4-
from textual.widget import Widget
4+
from textual.widget import Widget, WidgetError, MountError
55
from textual.widgets import Static
66

77
class SelfOwn(Widget):
@@ -16,7 +16,7 @@ async def test_mount_via_app() -> None:
1616
widgets = [Static(id=f"starter-{n}") for n in range( 10 )]
1717

1818
async with App().run_test() as pilot:
19-
with pytest.raises(Widget.WidgetError):
19+
with pytest.raises(WidgetError):
2020
await pilot.app.mount(SelfOwn())
2121

2222
async with App().run_test() as pilot:
@@ -92,16 +92,16 @@ async def test_mount_via_app() -> None:
9292
async with App().run_test() as pilot:
9393
# Make sure we get told off for trying to before and after.
9494
await pilot.app.mount_all(widgets)
95-
with pytest.raises(Static.MountError):
95+
with pytest.raises(MountError):
9696
await pilot.app.mount(Static(), before=2, after=2)
9797

9898
async with App().run_test() as pilot:
9999
# Make sure we get told off trying to mount relative to something
100100
# that isn't actually in the DOM.
101101
await pilot.app.mount_all(widgets)
102-
with pytest.raises(Static.MountError):
102+
with pytest.raises(MountError):
103103
await pilot.app.mount(Static(), before=Static())
104-
with pytest.raises(Static.MountError):
104+
with pytest.raises(MountError):
105105
await pilot.app.mount(Static(), after=Static())
106106

107107
# TODO: At the moment query_one() simply takes a query and returns the

0 commit comments

Comments
 (0)