Skip to content

👌 Copy edited tokens in SyntaxTreeNode.to_tokens instead of writing in place - #464

Merged
chrisjsewell merged 3 commits into
masterfrom
maint/tree-to-tokens-copy-on-write
Nov 9, 2026
Merged

chrisjsewell merged 3 commits into
masterfrom
maint/tree-to-tokens-copy-on-write

Conversation

@chrisjsewell

@chrisjsewell chrisjsewell commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

This follows up #453 by @KennyMcSimpson, which fixed SyntaxTreeNode.to_tokens() silently dropping edits to the children of inline and image nodes. The fix is kept; this changes how it writes the edits back, before the change is released.

As merged, to_tokens() assigns the rebuilt child list to the Token objects the tree shares with the stream it was built from. That has two side effects. The original token stream changes too, which it did not before #453. And a list already returned can change later: if two trees are built from the same tokens and one is edited, the edited tree's output reverts once the other tree calls to_tokens(), because the last call wins. The docstring and docs/using.md needed several sentences and a copy.deepcopy recipe to explain this.

With this change, a token is replaced by token.copy(children=...) only when its rebuilt children differ by identity from its current ones. Edits still appear in the output, the original tokens are left unchanged, and a returned list stays stable. An unedited round trip still returns the same Token objects, and now the same child lists as well. Copies propagate upward, so an edited image inside an inline token gives a new image token and a new inline token. The copy is shallow, so meta and a non-empty attrs are still shared, as they are for every token the tree reuses.

The test that pinned the in-place write is replaced by tests for each of these properties: edits reach the output, the original stream is unchanged, a returned list is stable after another tree's to_tokens(), an unedited round trip reuses the same objects, and a nested image edit propagates. All four new tests fail against the current in-place code, and the identity test fails if the copy is made unconditionally. Two more tests pin that edits are detected by identity rather than equality (a child replaced by an equal but distinct token reaches the output), and that the tree's children are the source of truth: a change made directly to a token's children list does not appear in the output. The docstring and the docs/using.md paragraph drop the shared-state caveats and say instead that the tree's children win, and that to_tokens() does not modify the token stream, although nodes share their Token objects with it, so setting a field such as node.token.content changes both.

The full test suite and pre-commit pass. MyST-Parser master's test suite (at e0a12c3) also passes against this branch (1345 passed, 11 skipped). Its compute_unique_slug is the one known caller that relies on to_tokens(), and it only reads the heading's inline children content.

…n place

#453 made 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 test that pinned the in-place write is replaced by tests for each of these
properties, and the docstring and docs/using.md no longer need the
shared-state caveats or the deepcopy advice.
…heck

The docstring and docs/using.md now say that the tree's children are the
source of truth, so changes made directly to a token's children list do not
appear in the output, and that to_tokens() itself does not modify the token
stream, although nodes share their Token objects with it.

The edit check uses all(map(operator.is_, ...)), which halves its overhead
on an unedited round trip. A new test replaces a child with an equal but
distinct token, which fails if the identity check becomes an equality check,
and another pins that direct changes to a token's children do not reach the
output.
@chrisjsewell
chrisjsewell merged commit 17b7227 into master Nov 9, 2026
15 checks passed
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.

1 participant