Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a public read-only parent property to TreeNode #1488

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.10.0] - Unreleased

### Added

- Added `TreeNode.parent` -- a read-only property for accessing a node's parent https://github.com/Textualize/textual/issues/1397

### Changed

- `MouseScrollUp` and `MouseScrollDown` now inherit from `MouseEvent` and have attached modifier keys. https://github.com/Textualize/textual/pull/1458
Expand All @@ -29,8 +33,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Widget.render_line now returns a Strip
- Fix for slow updates on Windows
- Bumped Rich dependency
- Bumped Rich dependency

## [0.8.2] - 2022-12-28

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def id(self) -> NodeID:
"""NodeID: Get the node ID."""
return self._id

@property
def parent(self) -> TreeNode[TreeDataType] | None:
"""TreeNode[TreeDataType] | None: The parent of the node."""
return self._parent

@property
def is_expanded(self) -> bool:
"""bool: Check if the node is expanded."""
Expand Down
10 changes: 10 additions & 0 deletions tests/tree/test_tree_node_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from textual.widgets import TreeNode, Tree

def test_tree_node_parent() -> None:
"""It should be possible to access a TreeNode's parent."""
tree = Tree[None]("Anakin")
child = tree.root.add("Leia")
grandchild = child.add("Ben")
assert tree.root.parent is None
assert grandchild.parent == child
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(apologies if this in nit'ting, I don't mean it as such) I think it might be better to change this == and the one below to is. Basically I submit that this is the contract -- you get back a node that IS the parent, not just "looks the same". Or at least that's what I think the contract should be.

assert child.parent == tree.root