Skip to content

Initialize children in Document.__init__()#209

Merged
frostming merged 2 commits into
frostming:masterfrom
apetresc:document-initialize-children
Oct 15, 2024
Merged

Initialize children in Document.__init__()#209
frostming merged 2 commits into
frostming:masterfrom
apetresc:document-initialize-children

Conversation

@apetresc

@apetresc apetresc commented Oct 4, 2024

Copy link
Copy Markdown
Contributor

I'm using Marko to parse a Markdown document, split it into multiple documents according to some headings, and then render each of those sub-documents separately. While in the process of doing this, I discovered a strange behaviour:

>>> from marko.block import Document
>>> d1 = Document()
>>> d1.children.append(1)
>>> d1
<Document children=[1]>
>>> d1.children
[1]
>>> d2 = Document()
>>> d2
<Document children=[1]>
>>> d2.children
[1]

Looking at Marko's code, it's because you define and assign children = [] as a class variable on BlockElement, and then rely on each subclass to re-initialize it. But, I guess since Document is meant to be virtual, it doesn't do that. So any manually-constructed Document will share the same instance of children, which I'm sure is not intended.

Perhaps what I'm missing is some other way to accomplish the goal of constructing a Document object (which can then be render()-ed) from a sequence of Nodes (filtered out of another Document), rather than from a string. But I don't think this is currently possible, so the only way to do this is to break the virtual-ness of Document and just initialize one. In which case, something like this PR is required.

@frostming

Copy link
Copy Markdown
Owner

Because not all block elements, like BlankLine, has children, the class attribute serves more as a type hint. It definitely needs to be clarified.

Since you are it, can you help remove the super().__init__() because there is nothing to inherit.

@apetresc

apetresc commented Oct 5, 2024

Copy link
Copy Markdown
Contributor Author

Since you are it, can you help remove the super().__init__() because there is nothing to inherit.

Done!

@frostming frostming merged commit 99c223c into frostming:master Oct 15, 2024
@apetresc

apetresc commented Apr 4, 2025

Copy link
Copy Markdown
Contributor Author

Are you planning to push a release with this fix? I'm not sure what your release cadence is, just wondering if I should continue monkey-patching my fix locally or just wait for this to go live. Thanks for the merge!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants