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

Additional support for subclassing of 'treelib.Node' #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,25 @@ class SubTree(Tree):
node = tree.create_node()
self.assertTrue(isinstance(node, SubNode))

def test_subclassing_extra_kwargs(self):
class SubNode(Node):
def __init__(self, some_argument=None, **kwargs):
self.some_property = some_argument
super().__init__(**kwargs)

class SubTree(Tree):
node_class = SubNode

tree = SubTree()
node = tree.create_node(some_argument="some_value")
self.assertTrue(isinstance(node, SubNode))
self.assertEqual(node.some_property, "some_value")

tree = Tree(node_class=SubNode)
node = tree.create_node(some_argument="some_value")
self.assertTrue(isinstance(node, SubNode))
self.assertEqual(node.some_property, "some_value")

def test_shallow_copy_hermetic_pointers(self):
# tree 1
# Hárry
Expand Down
4 changes: 2 additions & 2 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ def contains(self, nid):
"""Check if the tree contains node of given id"""
return True if nid in self._nodes else False

def create_node(self, tag=None, identifier=None, parent=None, data=None):
def create_node(self, tag=None, identifier=None, parent=None, data=None, **kwargs):
"""
Create a child node for given @parent node. If ``identifier`` is absent,
a UUID will be generated automatically.
"""
node = self.node_class(tag=tag, identifier=identifier, data=data)
node = self.node_class(tag=tag, identifier=identifier, data=data, **kwargs)
self.add_node(node, parent)
return node

Expand Down