Skip to content

🐛 Preserve inline child edits when converting syntax trees to tokens - #453

Merged
chrisjsewell merged 8 commits into
executablebooks:masterfrom
KennyMcSimpson:fix/syntax-tree-token-children
Nov 9, 2026
Merged

chrisjsewell merged 8 commits into
executablebooks:masterfrom
KennyMcSimpson:fix/syntax-tree-token-children

Conversation

@KennyMcSimpson

Copy link
Copy Markdown
Contributor

SyntaxTreeNode.to_tokens() includes changes to block children but silently drops changes below inline and image nodes. For example, clearing an inline node's children still renders the original paragraph, and clearing image children retains the original alternative text.

Rebuild token child lists from the current tree while keeping the original token objects. Add regression coverage for removal, reordering, nested edits and empty inline replacement, document the synchronization, and add a changelog entry.

Validation: the final regression tests produce 3 failures and 1 passing round-trip control on the original implementation. With this change, the complete Python 3.12 suite passes (1,035 tests), and all pre-commit hooks pass, including Ruff and strict mypy. Tests ran in UTF-8 mode on Windows.

Prepared with OpenAI Codex assistance; no independent human review is claimed.

@chrisjsewell
chrisjsewell merged commit 4d7bf9e into executablebooks:master Nov 9, 2026
15 checks passed
chrisjsewell added a commit that referenced this pull request Nov 9, 2026
…n place (#464)

#453 made `SyntaxTreeNode.to_tokens()` keep edits to the children of inline and image nodes by assigning the rebuilt child list to the shared Token objects. That also changed the token stream the tree was built from, and a list returned earlier could change again after another tree built from the same tokens called `to_tokens()`.

A token is now replaced by `token.copy(children=...)` only when its rebuilt children differ by identity from its current ones. Edits still reach the output, the original tokens are left unchanged, and an unedited round trip still returns the same Token objects and child lists. A copied image token makes the enclosing inline token's children differ, so it is copied too. The edit check compares the rebuilt children by identity, so a child replaced by an equal but distinct token still reaches the output.

The docstring and `docs/using.md` drop the shared-state caveats and the deepcopy advice. They now say that the tree's `children` are the source of truth, so a change made directly to a token's `children` list does not appear in the output, and that `to_tokens()` does not modify the token stream the tree was built from, although nodes share their `Token` objects with it.
@chrisjsewell

Copy link
Copy Markdown
Member

Merged in 4d7bf9e, thanks @KennyMcSimpson! Before merging I merged master in, dropped the changelog line (written at release time), parametrized the tests, and documented the behaviour.

One follow-up since: as merged, to_tokens() wrote the rebuilt children back onto the Token objects the tree shares with the stream it was built from, so the original stream changed too, and a list returned earlier could change after another tree's to_tokens(). #464 (17b7227) keeps your fix but replaces an edited token with token.copy(children=...) instead of writing in place: edits still reach the output, the original stream is left unchanged, and an unedited round trip still returns the same objects. Neither change has been released yet, so the release ships the copy-on-write version.

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