Skip to content

Commit 6a430a7

Browse files
docs: explain two-phase flow-sequence handling in sync() (#53)
* docs: explain two-phase flow-sequence handling in sync() * docs: refine two-phase flow-sequence comment in sync()
1 parent 05a1797 commit 6a430a7

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/yamltrip/document.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,16 @@ def sync(self, *keys: KeyPart, value: Any) -> Document:
468468
except (ValueError, KeyError):
469469
return self.upsert(*normalized, value=value)
470470

471-
# Pre-convert any flow sequences that will be modified.
472-
# This targets only the affected leaf paths, preserving sibling formatting.
471+
# Two-phase flow-sequence handling:
472+
#
473+
# Phase 1 (_flow_seq_replacements) pre-converts only the flow sequences
474+
# that will be modified. A full replace at a higher path would
475+
# re-serialize the whole mapping with the default indentation, losing
476+
# the document's existing indent for nested lists.
477+
#
478+
# Phase 2 (except PatchError below) catches cases phase 1 misses. The
479+
# known gap is list reordering: new indices in the diff weren't in the
480+
# original tree, so phase 1 couldn't anticipate them.
473481
doc: Document = self
474482
flow_patches = _flow_seq_replacements(
475483
self._core_doc, old_value, value, normalized
@@ -488,7 +496,8 @@ def sync(self, *keys: KeyPart, value: Any) -> Document:
488496
if _classify_patch_error(e) != _PatchErrorKind.BLOCK_SEQUENCE_EXPECTED:
489497
raise
490498
# Fallback: a flow sequence was missed by pre-detection (e.g. due to
491-
# list reordering). Replace the entire synced value.
499+
# list reordering). Full replace from original doc; phase 1 work
500+
# is superseded.
492501
route = _make_route(normalized)
493502
op = Op.replace(value)
494503
return self._apply_patches([Patch(route=route, operation=op)])

0 commit comments

Comments
 (0)