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)
When constructing panflute elements (e.g.
Para) the content elements have theparentnot set (i.e. it is set toNone.The
parentis first set when the elements are requested throughListContainer.__getitem__IMHO the state of the element should not depend on whether the element has been requested or not.
Is there any reason why the
parentis not set whenListContainer.__set_item__orListContainer.insertare called?This could be done calling
ListContainer.attachat the end ofListContainer.__set_item__orListContainer.insert:The same could be implemented for
DictContainer.__setitem__