Skip to content

parent is None until the element is retrieved via __get_item__ #255

Description

@me-kell

When constructing panflute elements (e.g. Para) the content elements have the parent not set (i.e. it is set to None.

The parent is first set when the elements are requested through ListContainer.__getitem__

>>> from panflute import Para, Str
>>> s = Str("x")
>>> p = Para(s)
>>> print(s.parent)
None # the parent is None
>>> p.content[0] # accessing the content elements will set the parent
Str(x)
>>> print(s.parent)
Para(Str(x))

IMHO the state of the element should not depend on whether the element has been requested or not.

Is there any reason why the parent is not set when ListContainer.__set_item__ or ListContainer.insert are called?

This could be done calling ListContainer.attach at the end of ListContainer.__set_item__ or ListContainer.insert:

    def __setitem__(self, i, v):
        if isinstance(i, slice):
            v = (check_type(x, self.oktypes) for x in v)
        else:
            v = check_type(v, self.oktypes)
        self.list[i] = v
        attach(self.list[i], self.parent, self.location, i) # call attach to set the parent

    def insert(self, i, v):
        v = check_type(v, self.oktypes)
        self.list.insert(i, v)
        attach(self.list[i], self.parent, self.location, i) # call attach to set the parent

The same could be implemented for DictContainer.__setitem__

    def __setitem__(self, k, v):
        v = check_type(v, self.oktypes)
        self.dict[k] = v
        attach(self.dict[k], self.parent, self.location)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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