Skip to content

Allow overriding of Node.parent? #280

@gregreich

Description

@gregreich

I regularly deal with problems where the children of a node are interconnected and only "make sense" together. I often need to verify and enforce this, using _{pre,post}_{attach,detach}_children(children). However, it is somewhat cumbersome to dis-allow children to be attached or detached individually using, for example, Node.parent = None (which would invalidate the tree at least temporarily, until the last child has been removed).

While I can work around this using a locking mechanism (for example, 1. set a locking variable in _pre_detach_children(children); 2. assert that variable in _pre_detach(parent); and 3. unset the variable in _post_detach_children(children)), it would be much cleaner if a user of anytree could override the default implementation of Node.parent to raise an exception.

For this, one would merely need an additional, thin layer of indirection with a protected internal parent property, say, Node._parent which is used for all internal for attaching and detaching nodes; Node.parent in its default implementation would just call Node._parent.

Any thoughts (or pointers to existing functionality I have missed), maybe @c0fec0de ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions