From 6b5015b266b0ff963eb1fd2d039bb2d62938ffe8 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 13 Feb 2023 15:09:40 +0000 Subject: [PATCH] mass renaming --- src/textual/_layout.py | 4 ++-- src/textual/app.py | 20 ++++++++++---------- src/textual/dom.py | 12 ++++++------ src/textual/walk.py | 10 +++++----- src/textual/widget.py | 28 +++++++++++++--------------- src/textual/widgets/_list_view.py | 18 +++++++++--------- tests/test_screens.py | 10 +++++----- tests/test_unmount.py | 2 +- tests/test_widget_child_moving.py | 20 ++++++++++---------- tests/test_widget_mount_point.py | 2 +- tests/test_widget_mounting.py | 24 ++++++++++++------------ tests/test_widget_removing.py | 22 +++++++++++----------- 12 files changed, 85 insertions(+), 87 deletions(-) diff --git a/src/textual/_layout.py b/src/textual/_layout.py index 5556a705e..5123d832e 100644 --- a/src/textual/_layout.py +++ b/src/textual/_layout.py @@ -57,7 +57,7 @@ class Layout(ABC): Returns: Width of the content. """ - if not widget.children: + if not widget._nodes: width = 0 else: # Use a size of 0, 0 to ignore relative sizes, since those are flexible anyway @@ -85,7 +85,7 @@ class Layout(ABC): Returns: Content height (in lines). """ - if not widget.children: + if not widget._nodes: height = 0 else: # Use a height of zero to ignore relative heights diff --git a/src/textual/app.py b/src/textual/app.py index 58f66afca..88030283c 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -419,7 +419,7 @@ class App(Generic[ReturnType], DOMNode): return self._return_value @property - def children_view(self) -> Sequence["Widget"]: + def children(self) -> Sequence["Widget"]: """A view on to the children which contains just the screen.""" try: return (self.screen,) @@ -1646,19 +1646,19 @@ class App(Generic[ReturnType], DOMNode): # Now to figure out where to place it. If we've got a `before`... if before is not None: # ...it's safe to NodeList._insert before that location. - parent.children._insert(before, child) + parent._nodes._insert(before, child) elif after is not None and after != -1: # In this case we've got an after. -1 holds the special # position (for now) of meaning "okay really what I mean is # do an append, like if I'd asked to add with no before or # after". So... we insert before the next item in the node # list, iff after isn't -1. - parent.children._insert(after + 1, child) + parent._nodes._insert(after + 1, child) else: # At this point we appear to not be adding before or after, # or we've got a before/after value that really means # "please append". So... - parent.children._append(child) + parent._nodes._append(child) # Now that the widget is in the NodeList of its parent, sort out # the rest of the admin. @@ -1703,8 +1703,8 @@ class App(Generic[ReturnType], DOMNode): raise AppError(f"Can't register {widget!r}; expected a Widget instance") if widget not in self._registry: self._register_child(parent, widget, before, after) - if widget.children: - self._register(widget, *widget.children) + if widget._nodes: + self._register(widget, *widget._nodes) apply_stylesheet(widget) if not self._running: @@ -1721,7 +1721,7 @@ class App(Generic[ReturnType], DOMNode): """ widget.reset_focus() if isinstance(widget._parent, Widget): - widget._parent.children._remove(widget) + widget._parent._nodes._remove(widget) widget._detach() self._registry.discard(widget) @@ -2104,7 +2104,7 @@ class App(Generic[ReturnType], DOMNode): # snipping each affected branch from the DOM. for widget in pruned_remove: if widget.parent is not None: - widget.parent.children._remove(widget) + widget.parent._nodes._remove(widget) # Return the list of widgets that should end up being sent off in a # prune event. @@ -2123,10 +2123,10 @@ class App(Generic[ReturnType], DOMNode): while stack: widget = pop() - children = [*widget.children, *widget._get_virtual_dom()] + children = [*widget._nodes, *widget._get_virtual_dom()] if children: yield children - for child in widget.children: + for child in widget._nodes: push(child) def _remove_nodes(self, widgets: list[Widget]) -> AwaitRemove: diff --git a/src/textual/dom.py b/src/textual/dom.py index 42478bf82..f83888d30 100644 --- a/src/textual/dom.py +++ b/src/textual/dom.py @@ -133,7 +133,7 @@ class DOMNode(MessagePump): check_identifiers("class name", *_classes) self._classes.update(_classes) - self.children: NodeList = NodeList() + self._nodes: NodeList = NodeList() self._css_styles: Styles = Styles(self) self._inline_styles: Styles = Styles(self) self.styles: RenderStyles = RenderStyles( @@ -152,9 +152,9 @@ class DOMNode(MessagePump): super().__init__() @property - def children_view(self) -> Sequence["Widget"]: + def children(self) -> Sequence["Widget"]: """A view on to the children.""" - return self.children + return self._nodes @property def auto_refresh(self) -> float | None: @@ -678,7 +678,7 @@ class DOMNode(MessagePump): Children of this widget which will be displayed. """ - return [child for child in self.children if child.display] + return [child for child in self._nodes if child.display] def watch( self, @@ -721,7 +721,7 @@ class DOMNode(MessagePump): Args: node: A DOM node. """ - self.children._append(node) + self._nodes._append(node) node._attach(self) def _add_children(self, *nodes: Widget) -> None: @@ -730,7 +730,7 @@ class DOMNode(MessagePump): Args: *nodes: Positional args should be new DOM nodes. """ - _append = self.children._append + _append = self._nodes._append for node in nodes: node._attach(self) _append(node) diff --git a/src/textual/walk.py b/src/textual/walk.py index 726af5e81..d43d40b06 100644 --- a/src/textual/walk.py +++ b/src/textual/walk.py @@ -53,7 +53,7 @@ def walk_depth_first( """ from textual.dom import DOMNode - stack: list[Iterator[DOMNode]] = [iter(root.children)] + stack: list[Iterator[DOMNode]] = [iter(root._nodes)] pop = stack.pop push = stack.append check_type = filter_type or DOMNode @@ -67,8 +67,8 @@ def walk_depth_first( else: if isinstance(node, check_type): yield node - if node.children_view: - push(iter(node.children_view)) + if node.children: + push(iter(node.children)) @overload @@ -122,9 +122,9 @@ def walk_breadth_first( if with_root and isinstance(root, check_type): yield root - extend(root.children_view) + extend(root.children) while queue: node = popleft() if isinstance(node, check_type): yield node - extend(node.children_view) + extend(node.children) diff --git a/src/textual/widget.py b/src/textual/widget.py index 197eb6147..0842aff93 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -299,7 +299,7 @@ class Widget(DOMNode): """ parent = self.parent if parent is not None: - siblings = list(parent.children) + siblings = list(parent._nodes) siblings.remove(self) return siblings else: @@ -390,7 +390,7 @@ class Widget(DOMNode): NoMatches: if no children could be found for this ID WrongType: if the wrong type was found. """ - child = self.children._get_by_id(id) + child = self._nodes._get_by_id(id) if child is None: raise NoMatches(f"No child found with id={id!r}") if expect_type is None: @@ -472,7 +472,7 @@ class Widget(DOMNode): """ assert self.is_container - cache_key = (size, self.children._updates) + cache_key = (size, self._nodes._updates) if ( self._arrangement_cache_key == cache_key and self._cached_arrangement is not None @@ -481,7 +481,7 @@ class Widget(DOMNode): self._arrangement_cache_key = cache_key arrangement = self._cached_arrangement = arrange( - self, self.children, size, self.screen.size + self, self._nodes, size, self.screen.size ) return arrangement @@ -549,7 +549,7 @@ class Widget(DOMNode): # children. We should be able to go looking for the widget's # location amongst its parent's children. try: - return cast("Widget", spot.parent), spot.parent.children.index(spot) + return cast("Widget", spot.parent), spot.parent._nodes.index(spot) except ValueError: raise MountError(f"{spot!r} is not a child of {self!r}") from None @@ -687,7 +687,7 @@ class Widget(DOMNode): """Ensure a given child reference is a Widget.""" if isinstance(child, int): try: - child = self.children[child] + child = self._nodes[child] except IndexError: raise WidgetError( f"An index of {child} for the child to {called} is out of bounds" @@ -696,7 +696,7 @@ class Widget(DOMNode): # We got an actual widget, so let's be sure it really is one of # our children. try: - _ = self.children.index(child) + _ = self._nodes.index(child) except ValueError: raise WidgetError(f"{child!r} is not a child of {self!r}") from None return child @@ -709,11 +709,11 @@ class Widget(DOMNode): # child; where we're moving it to, which should be within the child # list; and how we're supposed to move it. All that's left is doing # the right thing. - self.children._remove(child) + self._nodes._remove(child) if before is not None: - self.children._insert(self.children.index(target), child) + self._nodes._insert(self._nodes.index(target), child) else: - self.children._insert(self.children.index(target) + 1, child) + self._nodes._insert(self._nodes.index(target) + 1, child) # Request a refresh. self.refresh(layout=True) @@ -1181,9 +1181,7 @@ class Widget(DOMNode): List of widgets that can receive focus. """ - focusable = [ - child for child in self.children if child.display and child.visible - ] + focusable = [child for child in self._nodes if child.display and child.visible] return sorted(focusable, key=attrgetter("_focus_sort_key")) @property @@ -1277,7 +1275,7 @@ class Widget(DOMNode): Returns: True if this widget is a container. """ - return self.styles.layout is not None or bool(self.children) + return self.styles.layout is not None or bool(self._nodes) @property def is_scrollable(self) -> bool: @@ -1286,7 +1284,7 @@ class Widget(DOMNode): Returns: True if this widget may be scrolled. """ - return self.styles.layout is not None or bool(self.children) + return self.styles.layout is not None or bool(self._nodes) @property def layer(self) -> str: diff --git a/src/textual/widgets/_list_view.py b/src/textual/widgets/_list_view.py index 3d9db5892..76c0fdf05 100644 --- a/src/textual/widgets/_list_view.py +++ b/src/textual/widgets/_list_view.py @@ -94,33 +94,33 @@ class ListView(Vertical, can_focus=True, can_focus_children=False): """ if self.index is None: return None - elif 0 <= self.index < len(self.children): - return self.children[self.index] + elif 0 <= self.index < len(self._nodes): + return self._nodes[self.index] def validate_index(self, index: int | None) -> int | None: """Clamp the index to the valid range, or set to None if there's nothing to highlight.""" - if not self.children or index is None: + if not self._nodes or index is None: return None return self._clamp_index(index) def _clamp_index(self, index: int) -> int: """Clamp the index to a valid value given the current list of children""" - last_index = max(len(self.children) - 1, 0) + last_index = max(len(self._nodes) - 1, 0) return clamp(index, 0, last_index) def _is_valid_index(self, index: int | None) -> bool: """Return True if the current index is valid given the current list of children""" if index is None: return False - return 0 <= index < len(self.children) + return 0 <= index < len(self._nodes) def watch_index(self, old_index: int, new_index: int) -> None: """Updates the highlighting when the index changes.""" if self._is_valid_index(old_index): - old_child = self.children[old_index] + old_child = self._nodes[old_index] old_child.highlighted = False if self._is_valid_index(new_index): - new_child = self.children[new_index] + new_child = self._nodes[new_index] new_child.highlighted = True else: new_child = None @@ -166,7 +166,7 @@ class ListView(Vertical, can_focus=True, can_focus_children=False): def on_list_item__child_clicked(self, event: ListItem._ChildClicked) -> None: self.focus() - self.index = self.children.index(event.sender) + self.index = self._nodes.index(event.sender) self.post_message_no_wait(self.Selected(self, event.sender)) def _scroll_highlighted_region(self) -> None: @@ -175,4 +175,4 @@ class ListView(Vertical, can_focus=True, can_focus_children=False): self.scroll_to_widget(self.highlighted_child, animate=False) def __len__(self): - return len(self.children) + return len(self._nodes) diff --git a/tests/test_screens.py b/tests/test_screens.py index a92b0493f..f88367e8c 100644 --- a/tests/test_screens.py +++ b/tests/test_screens.py @@ -45,8 +45,8 @@ async def test_installed_screens(): async def test_screens(): app = App() # There should be nothing in the children since the app hasn't run yet + assert not app._nodes assert not app.children - assert not app.children_view app._set_active() with pytest.raises(ScreenStackError): @@ -63,8 +63,8 @@ async def test_screens(): app.install_screen(screen2, "screen2") # Installing a screen does not add it to the DOM + assert not app._nodes assert not app.children - assert not app.children_view # Check they are installed assert app.is_screen_installed("screen1") @@ -91,21 +91,21 @@ async def test_screens(): # Check it is current assert app.screen is screen1 # There should be one item in the children view - assert app.children_view == (screen1,) + assert app.children == (screen1,) # Switch to another screen app.switch_screen("screen2") # Check it has changed the stack and that it is current assert app.screen_stack == [screen2] assert app.screen is screen2 - assert app.children_view == (screen2,) + assert app.children == (screen2,) # Push another screen app.push_screen("screen3") assert app.screen_stack == [screen2, screen3] assert app.screen is screen3 # Only the current screen is in children_view - assert app.children_view == (screen3,) + assert app.children == (screen3,) # Pop a screen assert app.pop_screen() is screen3 diff --git a/tests/test_unmount.py b/tests/test_unmount.py index 6ef23f247..2301e4010 100644 --- a/tests/test_unmount.py +++ b/tests/test_unmount.py @@ -13,7 +13,7 @@ async def test_unmount(): class UnmountWidget(Container): def on_unmount(self, event: events.Unmount): unmount_ids.append( - f"{self.__class__.__name__}#{self.id}-{self.parent is not None}-{len(self.children)}" + f"{self.__class__.__name__}#{self.id}-{self.parent is not None}-{len(self._nodes)}" ) class MyScreen(Screen): diff --git a/tests/test_widget_child_moving.py b/tests/test_widget_child_moving.py index d9e15de66..9b99b3ae8 100644 --- a/tests/test_widget_child_moving.py +++ b/tests/test_widget_child_moving.py @@ -59,9 +59,9 @@ async def test_widget_move_child() -> None: container = Widget(*widgets) await pilot.app.mount(container) container.move_child(child, before=target) - assert container.children[0].id == "widget-1" - assert container.children[1].id == "widget-0" - assert container.children[2].id == "widget-2" + assert container._nodes[0].id == "widget-1" + assert container._nodes[1].id == "widget-0" + assert container._nodes[2].id == "widget-2" # Test the different permutations of moving one widget after another. perms = ((0, 1), (widgets[0], 1), (0, widgets[1]), (widgets[0], widgets[1])) @@ -70,22 +70,22 @@ async def test_widget_move_child() -> None: container = Widget(*widgets) await pilot.app.mount(container) container.move_child(child, after=target) - assert container.children[0].id == "widget-1" - assert container.children[1].id == "widget-0" - assert container.children[2].id == "widget-2" + assert container._nodes[0].id == "widget-1" + assert container._nodes[1].id == "widget-0" + assert container._nodes[2].id == "widget-2" # Test moving after a child after the last child. async with App().run_test() as pilot: container = Widget(*widgets) await pilot.app.mount(container) container.move_child(widgets[0], after=widgets[-1]) - assert container.children[0].id == "widget-1" - assert container.children[-1].id == "widget-0" + assert container._nodes[0].id == "widget-1" + assert container._nodes[-1].id == "widget-0" # Test moving after a child after the last child's numeric position. async with App().run_test() as pilot: container = Widget(*widgets) await pilot.app.mount(container) container.move_child(widgets[0], after=widgets[9]) - assert container.children[0].id == "widget-1" - assert container.children[-1].id == "widget-0" + assert container._nodes[0].id == "widget-1" + assert container._nodes[-1].id == "widget-0" diff --git a/tests/test_widget_mount_point.py b/tests/test_widget_mount_point.py index eebfdec5a..57e070df6 100644 --- a/tests/test_widget_mount_point.py +++ b/tests/test_widget_mount_point.py @@ -23,7 +23,7 @@ def test_find_dom_spot(): # Just as a quick double-check, make sure the main components are in # their intended place. - assert list(screen.children) == [header, body, footer] + assert list(screen._nodes) == [header, body, footer] # Now check that we find what we're looking for in the places we expect # to find them. diff --git a/tests/test_widget_mounting.py b/tests/test_widget_mounting.py index 666dca537..305fd7c82 100644 --- a/tests/test_widget_mounting.py +++ b/tests/test_widget_mounting.py @@ -26,72 +26,72 @@ async def test_mount_via_app() -> None: async with App().run_test() as pilot: # Mount the first one and make sure it's there. await pilot.app.mount(widgets[0]) - assert len(pilot.app.screen.children) == 1 - assert pilot.app.screen.children[0] == widgets[0] + assert len(pilot.app.screen._nodes) == 1 + assert pilot.app.screen._nodes[0] == widgets[0] # Mount the next 2 widgets via mount. await pilot.app.mount(*widgets[1:3]) - assert list(pilot.app.screen.children) == widgets[0:3] + assert list(pilot.app.screen._nodes) == widgets[0:3] # Finally mount the rest of the widgets via mount_all. await pilot.app.mount_all(widgets[3:]) - assert list(pilot.app.screen.children) == widgets + assert list(pilot.app.screen._nodes) == widgets async with App().run_test() as pilot: # Mount a widget before -1, which is "before the end". penultimate = Static(id="penultimate") await pilot.app.mount_all(widgets) await pilot.app.mount(penultimate, before=-1) - assert pilot.app.screen.children[-2] == penultimate + assert pilot.app.screen._nodes[-2] == penultimate async with App().run_test() as pilot: # Mount a widget after -1, which is "at the end". ultimate = Static(id="ultimate") await pilot.app.mount_all(widgets) await pilot.app.mount(ultimate, after=-1) - assert pilot.app.screen.children[-1] == ultimate + assert pilot.app.screen._nodes[-1] == ultimate async with App().run_test() as pilot: # Mount a widget before -2, which is "before the penultimate". penpenultimate = Static(id="penpenultimate") await pilot.app.mount_all(widgets) await pilot.app.mount(penpenultimate, before=-2) - assert pilot.app.screen.children[-3] == penpenultimate + assert pilot.app.screen._nodes[-3] == penpenultimate async with App().run_test() as pilot: # Mount a widget after -2, which is "before the end". penultimate = Static(id="penultimate") await pilot.app.mount_all(widgets) await pilot.app.mount(penultimate, after=-2) - assert pilot.app.screen.children[-2] == penultimate + assert pilot.app.screen._nodes[-2] == penultimate async with App().run_test() as pilot: # Mount a widget before 0, which is "at the start". start = Static(id="start") await pilot.app.mount_all(widgets) await pilot.app.mount(start, before=0) - assert pilot.app.screen.children[0] == start + assert pilot.app.screen._nodes[0] == start async with App().run_test() as pilot: # Mount a widget after 0. You get the idea... second = Static(id="second") await pilot.app.mount_all(widgets) await pilot.app.mount(second, after=0) - assert pilot.app.screen.children[1] == second + assert pilot.app.screen._nodes[1] == second async with App().run_test() as pilot: # Mount a widget relative to another via query. queue_jumper = Static(id="queue-jumper") await pilot.app.mount_all(widgets) await pilot.app.mount(queue_jumper, after="#starter-5") - assert pilot.app.screen.children[6] == queue_jumper + assert pilot.app.screen._nodes[6] == queue_jumper async with App().run_test() as pilot: # Mount a widget relative to another via query. queue_jumper = Static(id="queue-jumper") await pilot.app.mount_all(widgets) await pilot.app.mount(queue_jumper, after=widgets[5]) - assert pilot.app.screen.children[6] == queue_jumper + assert pilot.app.screen._nodes[6] == queue_jumper async with App().run_test() as pilot: # Make sure we get told off for trying to before and after. diff --git a/tests/test_widget_removing.py b/tests/test_widget_removing.py index 49b92e191..6c648d04c 100644 --- a/tests/test_widget_removing.py +++ b/tests/test_widget_removing.py @@ -13,28 +13,28 @@ async def test_remove_single_widget(): assert not widget.is_attached await pilot.app.mount(widget) assert widget.is_attached - assert len(pilot.app.screen.children) == 1 + assert len(pilot.app.screen._nodes) == 1 await pilot.app.query_one(Static).remove() assert not widget.is_attached - assert len(pilot.app.screen.children) == 0 + assert len(pilot.app.screen._nodes) == 0 async def test_many_remove_all_widgets(): """It should be possible to remove all widgets on a multi-widget screen.""" async with App().run_test() as pilot: await pilot.app.mount(*[Static() for _ in range(10)]) - assert len(pilot.app.screen.children) == 10 + assert len(pilot.app.screen._nodes) == 10 await pilot.app.query(Static).remove() - assert len(pilot.app.screen.children) == 0 + assert len(pilot.app.screen._nodes) == 0 async def test_many_remove_some_widgets(): """It should be possible to remove some widgets on a multi-widget screen.""" async with App().run_test() as pilot: await pilot.app.mount(*[Static(classes=f"is-{n%2}") for n in range(10)]) - assert len(pilot.app.screen.children) == 10 + assert len(pilot.app.screen._nodes) == 10 await pilot.app.query(".is-0").remove() - assert len(pilot.app.screen.children) == 5 + assert len(pilot.app.screen._nodes) == 5 async def test_remove_branch(): @@ -46,7 +46,7 @@ async def test_remove_branch(): Container(Container(Container(Container(Container(Static()))))), ) assert len(pilot.app.screen.walk_children(with_self=False)) == 13 - await pilot.app.screen.children[0].remove() + await pilot.app.screen._nodes[0].remove() assert len(pilot.app.screen.walk_children(with_self=False)) == 7 @@ -68,14 +68,14 @@ async def test_remove_move_focus(): async with App().run_test() as pilot: buttons = [Button(str(n)) for n in range(10)] await pilot.app.mount(Container(*buttons[:5]), Container(*buttons[5:])) - assert len(pilot.app.screen.children) == 2 + assert len(pilot.app.screen._nodes) == 2 assert len(pilot.app.screen.walk_children(with_self=False)) == 12 assert pilot.app.focused is None await pilot.press("tab") assert pilot.app.focused is not None assert pilot.app.focused == buttons[0] - await pilot.app.screen.children[0].remove() - assert len(pilot.app.screen.children) == 1 + await pilot.app.screen._nodes[0].remove() + assert len(pilot.app.screen._nodes) == 1 assert len(pilot.app.screen.walk_children(with_self=False)) == 6 assert pilot.app.focused is not None assert pilot.app.focused == buttons[9] @@ -95,7 +95,7 @@ async def test_widget_remove_order(): Removable(Removable(Removable(id="grandchild"), id="child"), id="parent") ) assert len(pilot.app.screen.walk_children(with_self=False)) == 3 - await pilot.app.screen.children[0].remove() + await pilot.app.screen._nodes[0].remove() assert len(pilot.app.screen.walk_children(with_self=False)) == 0 assert removals == ["grandchild", "child", "parent"]