anytree icon indicating copy to clipboard operation
anytree copied to clipboard

Error in post-attach does not undo the operation

Open kayjan opened this issue 2 years ago • 0 comments

Taking the read-only example and implementing checks on post_attach instead of pre_attach, it does not undo the operation that is supposed to be atomic.

Sample code to replicate the issue below,

from anytree import NodeMixin


class SampleNode(NodeMixin):
    def __init__(self, foo, parent=None):
        super(SampleNode, self).__init__()
        self.foo = foo
        self.readonly = False
        self.parent = parent
    def _post_attach(self, parent):
        if self.root.readonly:
            raise ValueError()
    def _post_detach(self, parent):
        if self.root.readonly:
            raise ValueError()

a = SampleNode("a")
a0 = SampleNode("a0", parent=a)
a1 = SampleNode("a1", parent=a)
a1a = SampleNode("a1a", parent=a1)
a2 = SampleNode("a2", parent=a)

assert a1.parent == a

a.readonly = True
with pytest.raises(ValueError):
    a1.parent = a2

a1.parent == a2 # returns True, it should undo the operation

kayjan avatar Nov 15 '22 16:11 kayjan