Skip to content

Commit e236288

Browse files
refactor: extract _with_flow_seq_fallback helper from append/extend_list
1 parent 0ba93e3 commit e236288

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

src/yamltrip/document.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
)
3131

3232
if TYPE_CHECKING:
33-
from collections.abc import Sequence
33+
from collections.abc import Callable, Sequence
3434

3535
from ._core import Feature
3636
from ._types import KeyPart
@@ -316,20 +316,8 @@ def remove(self, *keys: KeyPart, prune: bool = False) -> Document:
316316
def append(self, *keys: KeyPart, value: Any) -> Document:
317317
"""Append a single item to the sequence at path."""
318318
route = _make_route(keys)
319-
op = Op.append(value)
320-
patch = Patch(route=route, operation=op)
321-
try:
322-
return self._apply_patches([patch])
323-
except PatchError as e:
324-
kind = _classify_patch_error(e)
325-
if kind == _PatchErrorKind.FLOW_SEQUENCE:
326-
current = self[keys]
327-
new_list = [*list(current), value]
328-
replace_op = Op.replace(new_list)
329-
return self._apply_patches([Patch(route=route, operation=replace_op)])
330-
if kind == _PatchErrorKind.NOT_A_SEQUENCE:
331-
raise NodeTypeError(str(e)) from None
332-
raise
319+
patch = Patch(route=route, operation=Op.append(value))
320+
return self._with_flow_seq_fallback(keys, [patch], lambda lst: [*lst, value])
333321

334322
def insert(self, *keys: KeyPart, index: int, value: Any) -> Document:
335323
"""Insert an item at a specific position in the sequence at path.
@@ -361,15 +349,25 @@ def extend_list(self, *keys: KeyPart, values: Sequence[Any]) -> Document:
361349
return self
362350
route = _make_route(keys)
363351
patches = [Patch(route=route, operation=Op.append(v)) for v in values]
352+
return self._with_flow_seq_fallback(keys, patches, lambda lst: [*lst, *values])
353+
354+
def _with_flow_seq_fallback(
355+
self,
356+
keys: tuple[KeyPart, ...],
357+
patches: list[Patch],
358+
fallback_fn: Callable[[list[Any]], list[Any]],
359+
) -> Document:
360+
"""Apply patches, falling back to get→mutate→replace for flow sequences."""
364361
try:
365362
return self._apply_patches(patches)
366363
except PatchError as e:
367364
kind = _classify_patch_error(e)
368365
if kind == _PatchErrorKind.FLOW_SEQUENCE:
369-
current = self[keys]
370-
new_list = [*list(current), *values]
371-
replace_op = Op.replace(new_list)
372-
return self._apply_patches([Patch(route=route, operation=replace_op)])
366+
new_list = fallback_fn(list(self[keys]))
367+
route = _make_route(keys)
368+
return self._apply_patches(
369+
[Patch(route=route, operation=Op.replace(new_list))]
370+
)
373371
if kind == _PatchErrorKind.NOT_A_SEQUENCE:
374372
raise NodeTypeError(str(e)) from None
375373
raise

0 commit comments

Comments
 (0)