Skip to content

Commit

Permalink
Add read-only access to the children of a TreeNode
Browse files Browse the repository at this point in the history
See #1398.
  • Loading branch information
davep authored and rodrigogiraoserrao committed Jan 7, 2023
1 parent 2c827e1 commit 0029470
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added `TreeNode.parent` -- a read-only property for accessing a node's parent https://github.com/Textualize/textual/issues/1397
- Added public `TreeNode` label access via `TreeNode.label` https://github.com/Textualize/textual/issues/1396
- Added read-only public access to the children of a `TreeNode` via `TreeNode.children` https://github.com/Textualize/textual/issues/1398

### Changed

Expand Down
10 changes: 10 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .._segment_tools import line_crop, line_pad
from .._types import MessageTarget
from .._typing import TypeAlias
from .._collections import ImmutableSequence
from ..binding import Binding
from ..geometry import Region, Size, clamp
from ..message import Message
Expand Down Expand Up @@ -53,6 +54,10 @@ def _get_guide_width(self, guide_depth: int, show_root: bool) -> int:
return guides


class TreeNodes(ImmutableSequence["TreeNode[TreeDataType]"]):
"""An immutable collection of `TreeNode`."""


@rich.repr.auto
class TreeNode(Generic[TreeDataType]):
"""An object that represents a "node" in a tree control."""
Expand Down Expand Up @@ -91,6 +96,11 @@ def _reset(self) -> None:
self._selected_ = False
self._updates += 1

@property
def children(self) -> TreeNodes[TreeDataType]:
"""TreeNodes[TreeDataType]: The child nodes of a TreeNode."""
return TreeNodes(self._children)

@property
def line(self) -> int:
"""int: Get the line number for this node, or -1 if it is not displayed."""
Expand Down
32 changes: 32 additions & 0 deletions tests/tree/test_tree_node_children.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from textual.widgets import Tree, TreeNode

def label_of(node: TreeNode[None]):
"""Get the label of a node.
TODO: This is just a helper function to reduce the number of type
errors, which can and will be remove once this code is merged with a
version of main that also has the TreeNode.label PR merged.
"""
return str(node._label)


def test_tree_node_children() -> None:
"""A node's children property should act like an immutable list."""
CHILDREN=23
tree = Tree[None]("Root")
for child in range(CHILDREN):
tree.root.add(str(child))
assert len(tree.root.children)==CHILDREN
for child in range(CHILDREN):
assert label_of(tree.root.children[child]) == str(child)
assert label_of(tree.root.children[0]) == "0"
assert label_of(tree.root.children[-1]) == str(CHILDREN-1)
assert [label_of(node) for node in tree.root.children] == [str(n) for n in range(CHILDREN)]
assert [label_of(node) for node in tree.root.children[:2]] == [str(n) for n in range(2)]
with pytest.raises(TypeError):
tree.root.children[0] = tree.root.children[1]
with pytest.raises(TypeError):
del tree.root.children[0]
with pytest.raises(TypeError):
del tree.root.children[0:2]

0 comments on commit 0029470

Please sign in to comment.