Skip to content

Commit

Permalink
Fix #52.
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Jun 24, 2018
1 parent a92e702 commit a63719c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion anytree/node/nodemixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class NodeMixin(object):

__slots__ = ("__parent", "__children")

separator = "/"

u"""
Expand Down Expand Up @@ -121,7 +123,7 @@ def __attach(self, parent):
if parent is not None:
self._pre_attach(parent)
parentchildren = parent.__children_
assert not any ([child is self for child in parentchildren]), "Tree internal data is corrupt."
assert not any([child is self for child in parentchildren]), "Tree internal data is corrupt."
# ATOMIC START
parentchildren.append(self)
self.__parent = parent
Expand Down
22 changes: 21 additions & 1 deletion tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_node_parent_error():
with assert_raises(TreeError, "Parent node 'parent' is not of type 'NodeMixin'."):
Node("root", "parent")


def test_parent_child():
"""A tree parent and child attributes."""
root = Node("root")
Expand Down Expand Up @@ -191,6 +192,7 @@ def test_node_children_type():
with assert_raises(TreeError, "Cannot add non-node object 'string'. It is not a subclass of 'NodeMixin'."):
root.children = ["string"]


def test_node_children_multiple():

root = Node("root")
Expand Down Expand Up @@ -441,21 +443,23 @@ def test_anyname():
Node('/foo', parent=myroot)
eq_(str(myroot), "Node('/[1, 2, 3]')")


def test_node_kwargs():
"""Ticket #24."""

class MyNode(Node):

def __init__(self, name, parent=None, **kwargs):
super(MyNode, self).__init__(name, parent, **kwargs)

def _post_attach(self, parent):
print(self.my_attribute)


node_a = MyNode('A')
node_b = MyNode('B', node_a, my_attribute=True)
eq_(repr(node_b), "MyNode('/A/B', my_attribute=True)")


def test_hookups():
"""Hookup attributes #29."""

Expand Down Expand Up @@ -485,12 +489,14 @@ def _post_detach(self, parent):
node_b = MyNode('B', node_a) # attach B on A
node_b.parent = None # detach B from A


def test_any_node_parent_error():
"""Any Node Parent Error."""

with assert_raises(TreeError, "Parent node 'r' is not of type 'NodeMixin'."):
AnyNode("r")


def test_any_node():
"""Any Node."""
r = AnyNode()
Expand All @@ -506,6 +512,7 @@ def test_any_node():
eq_(repr(a), "AnyNode()")
eq_(repr(b), "AnyNode(foo=4)")


def test_eq_overwrite():
"""Node with overwritten __eq__."""
class EqOverwrittingNode(NodeMixin):
Expand All @@ -531,3 +538,16 @@ def __eq__(self, other):
eq_(a.b, 0)
eq_(b.a, 1)
eq_(b.b, 0)


def test_node_slots():
"""__slots__ compatibility."""
class MyNode(NodeMixin):
__slots__ = ('_name', )

def __init__(self, name):
self._name = name

n = MyNode('foo')
with assert_raises(AttributeError, "'MyNode' object has no attribute 'bar'"):
n.bar = 4

0 comments on commit a63719c

Please sign in to comment.