Skip to content

Commit a05cffa

Browse files
committed
runtime check read-only
1 parent dc71564 commit a05cffa

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/textual/_node_list.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class DuplicateIds(Exception):
1818
"""Raised when attempting to add a widget with an id that already exists."""
1919

2020

21+
class ReadOnlyError(AttributeError):
22+
"""Raise if you try to mutate the list."""
23+
24+
2125
@rich.repr.auto(angular=True)
2226
class NodeList(Sequence["Widget"]):
2327
"""
@@ -193,3 +197,10 @@ def __getitem__(self, index: slice) -> list[Widget]: ...
193197

194198
def __getitem__(self, index: int | slice) -> Widget | list[Widget]:
195199
return self._nodes[index]
200+
201+
def __getattr__(self, key: str) -> object:
202+
if key in {"clear", "append", "pop", "insert", "remove", "extend"}:
203+
raise ReadOnlyError(
204+
"Widget.children is read-only: use Widget.mount(...) or Widget.remove(...) to add or remove widgets"
205+
)
206+
raise AttributeError(key)

0 commit comments

Comments
 (0)