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 Tree.reset, which clears a Tree and then then resets the Tree.root's label and data to given values #1709

Merged
merged 5 commits into from
Feb 1, 2023

Conversation

davep
Copy link
Contributor

@davep davep commented Feb 1, 2023

See #1437 for background. While it would be ideal to allow for the complete emptying of a Tree, the root node is required (and it's part of the construction of a Tree). So, here, when clearing the Tree we optionally allow for a new label to be given.

Ideally we'll also allow for fresh data to be provided too; but there's a wrinkle there in knowing the difference between the data being None, and no data being provided (so the current root's data being carried over). Following the method of defaulting used in init would cause problems. As such, rather than roll all of this into one commit, this goes with the basic requirement and the solution for data will follow.

Note this also starts some unit tests for the clearing of a Tree.


Edit to add: following a bit of internal chat (see comments for the conclusions), we've decided to keep Tree.clear as ways and move this into Tree.reset, which makes things a wee bit cleaner on the data front too.

See Textualize#1437 for background. While it would be ideal to allow for the complete
emptying of a Tree, the root node is required (and it's part of the
construction of a Tree). So, here, when clearing the Tree we optionally
allow for a new label to be given.

Ideally we'll also allow for fresh data to be provided too; but there's a
wrinkle there in knowing the difference between the data being None, and no
data being provided (so the current root's data being carried over).
Following the method of defaulting used in __init__ would cause problems. As
such, rather than roll all of this into one commit, this goes with the basic
requirement and the solution for data will follow.

Note this also starts some unit tests for the clearing of a Tree.
@davep davep added enhancement New feature or request Task labels Feb 1, 2023
@davep davep self-assigned this Feb 1, 2023
@davep davep linked an issue Feb 1, 2023 that may be closed by this pull request
davep added 3 commits February 1, 2023 12:45
Just before doing the commit I decided to rename the test tree in the new
unit test for clearing down a tree. Of course I managed to name it in such a
way that it becomes special to pytest.

This fixes that.
@davep davep marked this pull request as ready for review February 1, 2023 14:01
@rodrigogiraoserrao
Copy link
Contributor

I think the behaviour we are implementing makes sense but belongs in a new method, perhaps called reset or repopulate.
A method clear should limit itself to clearing things.

If you pay me to wash your car, you don't expect to come back and see your car painted fresh with a different colour!

@@ -560,13 +560,20 @@ def get_label_width(self, node: TreeNode[TreeDataType]) -> int:
label = self.render_label(node, NULL_STYLE, NULL_STYLE)
return label.cell_len

def clear(self) -> None:
"""Clear all nodes under root."""
def clear(self, label: TextType | None = None, data: TreeDataType = ...) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

I get the limitation of not being able to clear the root and I think this is so "exceptional" that it should be made obvious from the name of the method, e.g., by renaming it to clear_children or clear_under_root.

Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

I like @rodrigogiraoserrao 's idea to have a clear method and a reset. Where clear did the bare minimum, but reset adds label and data. Feels cleaner.

It means that reset wouldn't need the ellipsis sentinel.

@davep
Copy link
Contributor Author

davep commented Feb 1, 2023

Sold.

After some internal discussion we've decided to keep `Tree.clear` as it was,
and add a `Tree.reset`, which does a `Tree.clear` but resets the label and
data of `Tree.root` to the values given, while mirroring how `Tree.__init__`
takes those parameters.
@davep davep changed the title Allow setting a new label when performing a clear on a Tree Add Tree.reset, which clears a Tree and then then resets the Tree.root's label and data to given values Feb 1, 2023
@davep davep merged commit 7995271 into Textualize:main Feb 1, 2023
@davep davep deleted the tree-complete-clear branch February 1, 2023 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Tree] Allow for the complete repopulation of a Tree.
3 participants