From 9ad39cd06987e5f738e0a91c7f03c0d42d99c5dd Mon Sep 17 00:00:00 2001 From: Marat K Date: Tue, 27 Oct 2026 08:31:22 +0100 Subject: [PATCH 1/5] feat(edit): add native and pure source mutations Expose insertion, removal and key renaming through the PyO3 bridge and standalone pure backend, with textual and literal-segment paths. Preserve source-first diagnostics, lossless splice boundaries, empty ancestors and semantic verification. Add independent source-byte and semantic regressions, publish the collision exception consistently, pin the verified Rust core and update generated EN/RU/ZH documentation. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/CI.yml | 4 +- CHANGELOG.md | 10 + Cargo.toml | 2 +- README.md | 32 +++ docs/ru/CHANGELOG.ru.md | 10 + docs/ru/README.ru.md | 32 +++ docs/zh/CHANGELOG.zh.md | 9 + docs/zh/README.zh.md | 26 +++ python/ktav/__init__.py | 94 +++++++- python/ktav/_core.pyi | 23 +- python/ktav/_pure/__init__.py | 68 ++++++ python/ktav/_pure/_edit_layout.py | 19 +- python/ktav/_pure/_edit_remove.py | 220 ++++++++++++++++++ python/ktav/_pure/_edit_rename.py | 134 +++++++++++ python/ktav/_pure/_edit_splice.py | 220 +++++++++++++++++- python/ktav/_pure/errors.py | 4 + python/ktav/pure.py | 14 ++ root-docs/CHANGELOG/source-edit09/body-1.md | 29 +++ root-docs/README/source-edit/body-1.md | 36 --- root-docs/README/source-edit/body-2.md | 75 ++++++ root-docs/README/source-edit/body-3.md | 57 +++++ root-docs/README/source-edit/meta.js | 2 +- src/lib.rs | 77 ++++++ .../test_insert_parameterized_regressions.py | 46 ++++ .../edit/test_insert_remaining_regressions.py | 131 +++++++++++ .../edit/test_insert_semantic_regressions.py | 85 +++++++ .../test_mutation_source_first_regressions.py | 85 +++++++ ...st_mutation_source_geometry_regressions.py | 113 +++++++++ tests/edit/test_pure_mutations_regressions.py | 113 +++++++++ tests/edit/test_source_mutations.py | 92 ++++++++ 30 files changed, 1808 insertions(+), 54 deletions(-) create mode 100644 python/ktav/_pure/_edit_remove.py create mode 100644 python/ktav/_pure/_edit_rename.py create mode 100644 root-docs/README/source-edit/body-2.md create mode 100644 root-docs/README/source-edit/body-3.md create mode 100644 tests/edit/test_insert_parameterized_regressions.py create mode 100644 tests/edit/test_insert_remaining_regressions.py create mode 100644 tests/edit/test_insert_semantic_regressions.py create mode 100644 tests/edit/test_mutation_source_first_regressions.py create mode 100644 tests/edit/test_mutation_source_geometry_regressions.py create mode 100644 tests/edit/test_pure_mutations_regressions.py create mode 100644 tests/edit/test_source_mutations.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 332362b..6a7f2f8 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -116,7 +116,7 @@ jobs: - name: Install test tooling run: python -m pip install --upgrade pip maturin pytest pytest-cov "tomli>=2.5.0; python_version < '3.11'" # Lock + provenance check before the native build compiles anything. - # The core is an IMMUTABLE git pin (Cargo.toml rev 447a00bd...); + # The core is an IMMUTABLE git pin (Cargo.toml rev 8b6b45ff...); # the verification step below FAILS CLOSED on rev/version drift — # it never skips unconditionally. - name: Generate Cargo.lock (no --locked) @@ -127,7 +127,7 @@ jobs: import re, sys from pathlib import Path - PIN = "447a00bd012e8d3f347c7e1eb59c3f0d9a78332f" + PIN = "8b6b45ffbd9e50e1588e8ab6b1ae5d422bbe8910" toml = Path("Cargo.toml").read_text(encoding="utf-8") m = re.search(r'rev\s*=\s*"([0-9a-f]{40})"', toml) if m is None: diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b7e5cb..3615b85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,16 @@ underlying Rust implementation, see byte offset into the path), `KtavNotFoundError`, and `KtavUnsupportedError`; the same ten-field envelope, `error` is one of `EditInvalidPath` / `EditNotFound` / `EditUnsupported`. +- `ktav.insert_value(source, path, value)` and + `ktav.insert_value_segments(source, segments, value)` add a new Object + member without overwriting an existing key. `ktav.remove_value(source, path)` + and `ktav.remove_value_segments(source, segments)` remove an existing + value (array elements shift; empty parent Objects remain). +- `ktav.rename_key(source, path, new_key)` and + `ktav.rename_key_segments(source, segments, new_key)` rename an Object + member in place; `new_key` is one literal key, not a dotted path. + All six APIs use lossless source splices, are available in native and + pure-Python backends, and reject unsupported structural cases explicitly. - Spec 0.9 corpus: the conformance runner now executes the `versions/0.9/tests` fixtures (162 invalid — 72 new `diagnostics09` EOF/malformed-UTF-8 diagnostics), validates `expected_location` diff --git a/Cargo.toml b/Cargo.toml index 9c53ba2..58c5d28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ crate-type = ["cdylib"] [dependencies] # Подготовка 0.9: immutable core pin с source-edit; пакет ещё не опубликован. -ktav = { git = "https://github.com/ktav-lang/rust", rev = "447a00bd012e8d3f347c7e1eb59c3f0d9a78332f" } +ktav = { git = "https://github.com/ktav-lang/rust", rev = "8b6b45ffbd9e50e1588e8ab6b1ae5d422bbe8910" } pyo3 = { version = "0.28", features = ["abi3-py39", "extension-module", "indexmap", "generate-import-lib"] } indexmap = "2" rustc-hash = "2" diff --git a/README.md b/README.md index b17d29b..2ffc7f9 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,38 @@ guessing. Long or multiline replacements unfold only the touched inline wrappers; untouched nested branches stay inline. Both the native and the pure backend implement the identical semantics. +### Structural source edits — insert, remove, rename + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# Literal segments avoid path escaping: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + +`insert_value` and `remove_value` take the same textual paths as +`edit_value`; the `_segments` forms take already-decoded literal +segments. Insert's final segment names a new Object key: its parent must +exist and be an Object, and an existing key is never overwritten. Array +insertion is unsupported. Remove accepts an existing value, including +an array element (later elements shift); emptied parent Objects remain +rather than being pruned. Rename addresses an Object member and takes +`new_key` as one literal name, not a path; dots and backslashes there +are ordinary characters. The member's position and value stay put, and +a name collision is rejected. + +These operations splice source text rather than serializing the whole +document. Existing layout and unrelated text are preserved whenever a +lossless patch is possible; unsupported structural cases fail explicitly +instead of guessing. Empty segment lists and missing targets are errors. +The insert, remove and rename APIs are available with the native +backend. The pure-Python backend implements insert, remove and rename (including +`insert_value_segments`); their current readiness and conformance are pending CI +verification. + ### Source-edit cookbook These examples describe the unreleased 0.9 API, not the published 0.8.1 package. They replace existing values only: keys are not renamed, missing paths are not created, and entries are not removed. Each snippet is independent; Rust `?` assumes an enclosing `Result`-returning function. Go examples panic only for these fixed, known-valid inputs; applications should handle the returned error. diff --git a/docs/ru/CHANGELOG.ru.md b/docs/ru/CHANGELOG.ru.md index 60bebaf..a445f02 100644 --- a/docs/ru/CHANGELOG.ru.md +++ b/docs/ru/CHANGELOG.ru.md @@ -25,6 +25,16 @@ путь), `KtavNotFoundError` и `KtavUnsupportedError`; тот же десятипольный конверт, `error` — один из `EditInvalidPath` / `EditNotFound` / `EditUnsupported`. +- `ktav.insert_value(source, path, value)` и + `ktav.insert_value_segments(source, segments, value)` добавляют новый + член Object без перезаписи существующего ключа. `ktav.remove_value(source, path)` + и `ktav.remove_value_segments(source, segments)` удаляют существующее + значение (элементы массива сдвигаются; пустые родительские Object остаются). +- `ktav.rename_key(source, path, new_key)` и + `ktav.rename_key_segments(source, segments, new_key)` переименовывают член Object + на месте; `new_key` — одно буквальное имя, а не dotted-путь. + Все шесть API выполняют lossless-splice исходника, доступны в native и + pure-Python backend и явно отклоняют неподдерживаемые структурные случаи. - Корпус spec 0.9: conformance runner исполняет fixtures из `versions/0.9/tests` (162 invalid — 72 новых диагностики `diagnostics09` EOF/битый UTF-8), проверяет метаданные diff --git a/docs/ru/README.ru.md b/docs/ru/README.ru.md index 77fb589..4f2b1b9 100644 --- a/docs/ru/README.ru.md +++ b/docs/ru/README.ru.md @@ -212,6 +212,38 @@ byte-identical no-op, даже при неканоничном написани нетронутые ветки остаются inline. Native и pure backend реализуют идентичную семантику. +### Структурные правки исходника — вставка, удаление, переименование + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# Literal-сегменты не требуют экранирования пути: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + +`insert_value` и `remove_value` принимают текстовые пути как +`edit_value`; варианты `_segments` принимают уже декодированные +literal-сегменты. Последний сегмент вставки задаёт новый ключ Object: +родитель должен существовать и быть Object, существующий ключ никогда +не перезаписывается. Вставка в Array не поддерживается. Удаление +принимает существующее значение, включая элемент массива (последующие +элементы сдвигаются); опустевшие родительские Object сохраняются. +Переименование адресует член Object, а `new_key` — одно буквальное имя, +не путь: точки и обратные косые черты в нём являются обычными символами. +Позиция и значение члена сохраняются; конфликт имени отклоняется. + +Эти операции выполняют splice исходника, а не сериализуют весь документ. +Существующая раскладка и посторонний текст сохраняются, если возможна +lossless-правка; неподдерживаемые структурные случаи явно завершаются +ошибкой вместо догадок. Пустой список сегментов и отсутствующая цель — +ошибки. API вставки, удаления и переименования доступны с native +backend. Pure-Python backend реализует вставку, удаление и переименование +(включая `insert_value_segments`); их текущая готовность и соответствие +ожидают проверки CI. + ### Практические примеры правки исходника Примеры относятся к ещё не выпущенному API 0.9, а не к опубликованному пакету 0.8.1. Они заменяют только существующие значения: не переименовывают ключи, не создают отсутствующие пути и не удаляют записи. Фрагменты независимы; Rust `?` предполагает внешнюю функцию с результатом `Result`. В Go panic используется только для фиксированных корректных данных примера; приложение должно обработать возвращённую ошибку. diff --git a/docs/zh/CHANGELOG.zh.md b/docs/zh/CHANGELOG.zh.md index c704bbd..3e1ccee 100644 --- a/docs/zh/CHANGELOG.zh.md +++ b/docs/zh/CHANGELOG.zh.md @@ -22,6 +22,15 @@ Rust 实现见 [`ktav-lang/rust`](https://github.com/ktav-lang/rust)。 UTF-8 字节偏移十进制串)、`KtavNotFoundError` 与 `KtavUnsupportedError`;信封仍是同样的十字段,`error` 取 `EditInvalidPath` / `EditNotFound` / `EditUnsupported` 之一。 +- `ktav.insert_value(source, path, value)` 与 + `ktav.insert_value_segments(source, segments, value)` 新增 Object 成员, + 不覆盖已有键。`ktav.remove_value(source, path)` 与 + `ktav.remove_value_segments(source, segments)` 删除已有值(数组元素前移, + 空的父 Object 保留)。 +- `ktav.rename_key(source, path, new_key)` 与 + `ktav.rename_key_segments(source, segments, new_key)` 原位重命名 Object 成员; + `new_key` 是一个字面键名,而非点分路径。六个 API 均通过无损源文本 splice + 实现,native 与 pure-Python 后端均可用,并会明确拒绝不支持的结构情况。 - spec 0.9 语料:conformance 运行器现在执行 `versions/0.9/tests` 的 fixture(162 个 invalid —— 其中 72 个是新的 `diagnostics09` EOF/坏 UTF-8 诊断),校验 `expected_location` 元数据(line、 diff --git a/docs/zh/README.zh.md b/docs/zh/README.zh.md index 1312797..0ba0522 100644 --- a/docs/zh/README.zh.md +++ b/docs/zh/README.zh.md @@ -201,6 +201,32 @@ dotted/reopen 目标、结构替换下的内部注释、或在目标上下文中 替换只展开被触及的 inline 包装层;未触及的嵌套分支保持 inline。 native 与 pure backend 实现完全一致的语义。 +### 结构化源文本编辑 —— 插入、删除与重命名 + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# literal 段无需路径转义: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + +`insert_value` 和 `remove_value` 接受与 `edit_value` 相同的文本路径; +`_segments` 形式接受已解码的 literal 段。插入路径的最后一段是新 +Object 键:父级必须存在且为 Object,已有键绝不会被覆盖。不支持向 +Array 插入。删除接受既有值,包括数组元素(后续元素会前移);变空 +的父 Object 不会被裁掉。重命名定位 Object 成员,`new_key` 是单个 +literal 名称而非路径,其中的点号和反斜杠都是普通字符。成员位置与 +值保持不变;名称冲突会被拒绝。 + +这些操作通过 splice 修改源文本,而不是序列化整个文档。只要存在 +无损补丁,就保留已有布局及无关文本;不支持的结构情形会明确失败, +不会猜测。空段列表和不存在的目标均为错误。插入、删除与重命名 API +可在 native backend 中使用。Pure-Python backend 已实现插入、删除与重命名(包括 +`insert_value_segments`);其当前就绪度与符合预期情况仍待 CI 验证。 + ### 源文本编辑示例 以下示例适用于尚未发布的 0.9 API,而非已发布的 0.8.1 包。只能替换已有值:不会重命名键、创建缺失路径或删除条目。各代码片段相互独立;Rust 的 `?` 假定外层函数返回 `Result`。Go 示例仅针对固定且有效的输入使用 panic;应用程序应处理返回的错误。 diff --git a/python/ktav/__init__.py b/python/ktav/__init__.py index 5513d15..47a7f9f 100644 --- a/python/ktav/__init__.py +++ b/python/ktav/__init__.py @@ -25,6 +25,7 @@ if TYPE_CHECKING: from ktav._core import ( + KtavAlreadyExistsError, KtavDecodeError, KtavEditError, KtavEncodeError, @@ -49,6 +50,12 @@ ) from ktav._core import ( edit_value_segments as _edit_value_segments, + insert_value as _insert_value, + insert_value_segments as _insert_value_segments, + remove_value as _remove_value, + remove_value_segments as _remove_value_segments, + rename_key as _rename_key, + rename_key_segments as _rename_key_segments, ) from ktav._core import ( emit_canonical as _emit_canonical, @@ -64,6 +71,7 @@ ) _CORE_NAMES = ( + "KtavAlreadyExistsError", "KtavDecodeError", "KtavEditError", "KtavEncodeError", @@ -78,6 +86,12 @@ "dumps_force_strings", "edit_value", "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", "emit_canonical", "format", "loads", @@ -89,12 +103,19 @@ "dumps_force_strings", "edit_value", "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", "emit_canonical", "format", "loads", "loads_strict", ) _ERROR_CLASSES = ( + "KtavAlreadyExistsError", "KtavDecodeError", "KtavEditError", "KtavEncodeError", @@ -109,7 +130,7 @@ def _no_native(name: str) -> Any: def stub(*args: Any, **kwargs: Any) -> Any: - if name in ("edit_value", "edit_value_segments"): + if name in ("edit_value", "edit_value_segments", "insert_value", "insert_value_segments", "remove_value", "remove_value_segments", "rename_key", "rename_key_segments"): raise ImportError( f"ktav.{name} requires a ktav._core built against the" " source-edit 0.9 core; the installed extension does not" @@ -138,6 +159,7 @@ def _load_core() -> bool: _core = import_module("._core", __name__) except ImportError: from ktav._pure import ( + KtavAlreadyExistsError, KtavDecodeError, KtavEditError, KtavEncodeError, @@ -150,6 +172,7 @@ def _load_core() -> bool: ) globals().update( + KtavAlreadyExistsError=KtavAlreadyExistsError, KtavDecodeError=KtavDecodeError, KtavEditError=KtavEditError, KtavEncodeError=KtavEncodeError, @@ -201,6 +224,7 @@ def __getattr__(name: str) -> Any: __all__ = [ + "KtavAlreadyExistsError", "KtavDecodeError", "KtavEditError", "KtavEncodeError", @@ -216,6 +240,12 @@ def __getattr__(name: str) -> Any: "dumps_force_strings", "edit_value", "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", "emit_canonical", "format", "load", @@ -381,7 +411,9 @@ def edit_value(source: str | bytes | bytearray, path: str, replacement: Any) -> :class:`KtavUnsupportedError` when no lossless splice exists. """ _load_core() - return _edit_value(_as_text(source), path, replacement) + text = _as_text(source) + _loads(text) + return _edit_value(text, path, replacement) def _backend_message_error(message: str) -> Any: @@ -411,17 +443,65 @@ def edit_value_segments(source: str | bytes | bytearray, segments: Any, replacem :class:`KtavNotFoundError`. """ _load_core() - decoded = list(segments) # materialize once — segments may be a generator text = _as_text(source) + _loads(text) + decoded = list(segments) # materialize once — segments may be a generator if any(not isinstance(segment, str) for segment in decoded): - # Source first: on a broken source the real parser envelope wins - # over the transport error; the whole parse runs only on this - # rejected path (the entry op parses on the accepted path). - loads(text) raise _backend_message_error("edit path segments must be strings") return _edit_value_segments(text, decoded, replacement) +def insert_value(source: str | bytes | bytearray, path: str, replacement: Any) -> str: + _load_core() + text = _as_text(source) + _loads(text) + return _insert_value(text, path, replacement) + + +def insert_value_segments(source: str | bytes | bytearray, segments: Any, replacement: Any) -> str: + _load_core() + text = _as_text(source) + _loads(text) + decoded = list(segments) + if any(not isinstance(segment, str) for segment in decoded): + raise _backend_message_error("edit path segments must be strings") + return _insert_value_segments(text, decoded, replacement) + + +def remove_value(source: str | bytes | bytearray, path: str) -> str: + _load_core() + text = _as_text(source) + _loads(text) + return _remove_value(text, path) + + +def remove_value_segments(source: str | bytes | bytearray, segments: Any) -> str: + _load_core() + text = _as_text(source) + _loads(text) + decoded = list(segments) + if any(not isinstance(segment, str) for segment in decoded): + raise _backend_message_error("edit path segments must be strings") + return _remove_value_segments(text, decoded) + + +def rename_key(source: str | bytes | bytearray, path: str, new_key: str) -> str: + _load_core() + text = _as_text(source) + _loads(text) + return _rename_key(text, path, new_key) + + +def rename_key_segments(source: str | bytes | bytearray, segments: Any, new_key: str) -> str: + _load_core() + text = _as_text(source) + _loads(text) + decoded = list(segments) + if any(not isinstance(segment, str) for segment in decoded): + raise _backend_message_error("edit path segments must be strings") + return _rename_key_segments(text, decoded, new_key) + + def load(fp: IO[Any]) -> Any: """Parse a Ktav document read from a file-like object. diff --git a/python/ktav/_core.pyi b/python/ktav/_core.pyi index 1a2115a..684c3c3 100644 --- a/python/ktav/_core.pyi +++ b/python/ktav/_core.pyi @@ -105,6 +105,24 @@ def edit_value(source: str, path: str, replacement: Any) -> str: def edit_value_segments(source: str, segments: list[str], replacement: Any) -> str: """:func:`edit_value` with already-decoded literal path segments.""" +def insert_value(source: str, path: str, replacement: Any) -> str: + """Insert a new Object member; existing keys are never overwritten.""" + +def insert_value_segments(source: str, segments: list[str], replacement: Any) -> str: + """Insert using already-decoded literal path segments.""" + +def remove_value(source: str, path: str) -> str: + """Remove the value at a textual path.""" + +def remove_value_segments(source: str, segments: list[str]) -> str: + """Remove using already-decoded literal path segments.""" + +def rename_key(source: str, path: str, new_key: str) -> str: + """Rename an Object member while preserving its position and value.""" + +def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: + """Rename using already-decoded literal path segments.""" + class KtavError(Exception): """Base class for every exception raised by this library. @@ -149,7 +167,7 @@ class KtavEditError(KtavError): """Base class for source-edit failures. ``error`` is ``"EditInvalidPath"`` / ``"EditNotFound"`` / - ``"EditUnsupported"``; ``body`` is the decimal UTF-8 byte offset + ``"EditAlreadyExists"`` / ``"EditUnsupported"``; ``body`` is the decimal UTF-8 byte offset into the path argument for ``EditInvalidPath`` and the limitation reason for ``EditUnsupported``; every other envelope field stays ``None``. @@ -161,5 +179,8 @@ class KtavInvalidPathError(KtavEditError): class KtavNotFoundError(KtavEditError): """The path does not address an existing value.""" +class KtavAlreadyExistsError(KtavEditError): + """An insertion or rename would collide with an existing Object key.""" + class KtavUnsupportedError(KtavEditError): """No lossless single-range splice exists for this edit.""" diff --git a/python/ktav/_pure/__init__.py b/python/ktav/_pure/__init__.py index 679a615..65970ad 100644 --- a/python/ktav/_pure/__init__.py +++ b/python/ktav/_pure/__init__.py @@ -29,6 +29,7 @@ from ._text import validate_utf8 from .errors import ( + KtavAlreadyExistsError, KtavDecodeError, KtavEditError, KtavEncodeError, @@ -59,6 +60,7 @@ def _as_str(s: str | bytes | bytearray | memoryview) -> str: __all__ = [ + "KtavAlreadyExistsError", "KtavDecodeError", "KtavEditError", "KtavEncodeError", @@ -73,6 +75,12 @@ def _as_str(s: str | bytes | bytearray | memoryview) -> str: "dumps_force_strings", "edit_value", "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", "emit_canonical", "format", "loads", @@ -170,6 +178,66 @@ def edit_value_segments( return apply_edit(text, decoded, to_value(replacement)) +def insert_value(source: str | bytes | bytearray | memoryview, path: str, value: Any) -> str: + from ._edit import parse_path + + text = _as_str(source) + parse_document(text) + return insert_value_segments(text, parse_path(path), value) + + +def insert_value_segments(source: str | bytes | bytearray | memoryview, segments: Any, value: Any) -> str: + from ._edit_splice import insert_value_segments as _insert + + text = _as_str(source) + parse_document(text) + decoded = list(segments) + for segment in decoded: + if not isinstance(segment, str): + raise KtavEditError("edit path segments must be strings", error="Message") + return _insert(text, decoded, value) + + +def remove_value(source: str | bytes | bytearray | memoryview, path: str) -> str: + from ._edit import parse_path + + text = _as_str(source) + parse_document(text) + return remove_value_segments(text, parse_path(path)) + + +def remove_value_segments(source: str | bytes | bytearray | memoryview, segments: Any) -> str: + from ._edit_remove import remove_value_segments as _remove + + text = _as_str(source) + parse_document(text) + decoded = list(segments) + for segment in decoded: + if not isinstance(segment, str): + raise KtavEditError("edit path segments must be strings", error="Message") + return _remove(text, decoded) + + +def rename_key(source: str | bytes | bytearray | memoryview, path: str, new_key: str) -> str: + from ._edit import parse_path + + text = _as_str(source) + parse_document(text) + return rename_key_segments(text, parse_path(path), new_key) + + +def rename_key_segments(source: str | bytes | bytearray | memoryview, segments: Any, new_key: str) -> str: + from ._edit_rename import rename_key_segments as _rename + + text = _as_str(source) + parse_document(text) + decoded = list(segments) + for segment in decoded: + if not isinstance(segment, str): + raise KtavEditError("edit path segments must be strings", error="Message") + return _rename(text, decoded, new_key) + + def load(fp: Any) -> Any: """Parse a Ktav document read from a file-like object (text or. diff --git a/python/ktav/_pure/_edit_layout.py b/python/ktav/_pure/_edit_layout.py index 5546f63..033e959 100644 --- a/python/ktav/_pure/_edit_layout.py +++ b/python/ktav/_pure/_edit_layout.py @@ -43,9 +43,11 @@ def _layout_adjust( path: list[str], replacement: Any, splice: tuple[int, int, int], + *, + trim_owner_trailing: bool = False, ) -> str: frm, to, inserted = splice - owner = _inline_owner(source, root, path, frm) + owner = _inline_owner(source, root, path, frm, trim_owner_trailing) if owner is None: return candidate start, end = owner @@ -76,13 +78,20 @@ def _slot_body(slot: tuple[Any, ...]) -> tuple[int, int]: return slot[2], slot[3] -def _inline_owner(source: str, root: Any, path: list[str], pos: int) -> tuple[int, int] | None: +def _inline_owner(source: str, root: Any, path: list[str], pos: int, trim_trailing: bool = False) -> tuple[int, int] | None: node = root if root.inline: line_start, _line_end, raw_line = _physical_line_span(source, pos) lead = len(raw_line) - len(raw_line.lstrip(WS_CHARS)) stripped = raw_line[lead:].lstrip("") - start = line_start + lead + (len(raw_line) - lead - len(stripped)) + if trim_trailing: + stripped = stripped.rstrip(WS_CHARS) + bom_end = lead + (len(raw_line) - lead - len(raw_line[lead:].lstrip(""))) + if trim_trailing: + start = bom_end + (len(raw_line[bom_end:]) - len(raw_line[bom_end:].lstrip(WS_CHARS))) + end = len(raw_line.rstrip(WS_CHARS)) + return line_start + start, line_start + end + start = line_start + bom_end return start, start + len(stripped) for segment in path: if node.kind == "object": @@ -112,7 +121,9 @@ def _inline_owner(source: str, root: Any, path: list[str], pos: int) -> tuple[in line_start, _line_end, raw_line = _physical_line_span(source, pos) lead = len(raw_line) - len(raw_line.lstrip(WS_CHARS)) trimmed_line = raw_line[lead:].lstrip("") - base_shift = lead + (len(raw_line) - lead - len(trimmed_line)) + if trim_trailing: + trimmed_line = trimmed_line.rstrip(WS_CHARS) + base_shift = lead + (len(raw_line) - lead - len(raw_line[lead:].lstrip(""))) tail_body: str | None = None if _is_compound(trimmed_line): tail_body = trimmed_line diff --git a/python/ktav/_pure/_edit_remove.py b/python/ktav/_pure/_edit_remove.py new file mode 100644 index 0000000..7abb30e --- /dev/null +++ b/python/ktav/_pure/_edit_remove.py @@ -0,0 +1,220 @@ +"""Pure-Python lossless positional removal edits.""" +from __future__ import annotations + +from typing import Any + +from ._edit_common import ( + _lookup_value, _not_found, _parse_index, _same, _unsupported, + _physical_line_span, _inline_segments, +) +from ._edit_tree import ( + _build_tree, _descend, _collect_multiline_scalar_ranges, _has_comment_line, +) +from .writer import push_key_segment +from ._inline import ScanError +from .errors import KtavDecodeError, KtavEditError +from .parser import parse_document +from ._text import WS_CHARS, WS_INLINE_CHARS + +__all__ = ["remove_value_segments"] + + +def _clone(value: Any) -> Any: + if isinstance(value, dict): + return {key: _clone(child) for key, child in value.items()} + if isinstance(value, list): + return [_clone(child) for child in value] + return value + + +def _line_end(source: str, pos: int) -> int: + i = pos + while i < len(source) and source[i] not in "\r\n": + i += 1 + if i < len(source) and source[i] == "\r": + i += 1 + if i < len(source) and source[i] == "\n": + i += 1 + elif i < len(source): + i += 1 + return i + + +def _line_start(source: str, pos: int) -> int: + return max(source.rfind("\n", 0, pos), source.rfind("\r", 0, pos)) + 1 + + +def _expected_without(original: Any, segments: list[str]) -> Any: + expected = _clone(original) + parent = expected + for seg in segments[:-1]: + if isinstance(parent, dict): + if seg not in parent: + raise _not_found() + parent = parent[seg] + elif isinstance(parent, list): + idx = _parse_index(seg) + if idx is None or idx >= len(parent): + raise _not_found() + parent = parent[idx] + else: + raise _not_found() + key = segments[-1] + if isinstance(parent, dict): + if key not in parent: + raise _not_found() + del parent[key] + elif isinstance(parent, list): + idx = _parse_index(key) + if idx is None or idx >= len(parent): + raise _not_found() + del parent[idx] + else: + raise _not_found() + # Keep empty intermediate objects: no-prune semantics. + return expected + + +def _inline_parent(source: str, root: Any, segments: list[str], explicit: bool, anchor: int) -> tuple[int, int]: + parent, _inline, _first = _descend(root, segments[:-1], explicit) + if parent.slot is not None: + slot = parent.slot + if slot[0] != "block": + return slot[2], slot[3] + if not segments[:-1] and root.inline: + a, b, line = _physical_line_span(source, _line_start(source, anchor)) + lead = len(line) - len(line.lstrip(WS_CHARS)) + content = line[lead:] + bom = 1 if content.startswith("\ufeff") else 0 + content = content[bom:] + after_bom_lead = len(content) - len(content.lstrip(WS_CHARS)) + content = content[after_bom_lead:] + content = content.rstrip(WS_CHARS) + start = a + lead + bom + after_bom_lead + return start, start + len(content) + raise _unsupported("inline target without inline parent") + + +def _inline_range(source: str, root: Any, segments: list[str], explicit: bool, slot: tuple[Any, ...]) -> tuple[int, int]: + anchor = slot[1] + p0, p1 = _inline_parent(source, root, segments, explicit, anchor) + body = source[p0:p1] + if not body.endswith("}") and not body.endswith("]"): + raise _unsupported("invalid inline parent range") + try: + spans = _inline_segments(body) + except (ScanError, ValueError): + raise _unsupported("cannot split inline parent for removal") from None + parts: list[tuple[int, int]] = [] + for a, b in spans: + raw = body[a:b] + left = len(raw) - len(raw.lstrip(WS_INLINE_CHARS)) + right = len(raw.rstrip(WS_INLINE_CHARS)) + if left < right: + parts.append((p0 + a + left, p0 + a + right)) + pos = next((i for i, (a, b) in enumerate(parts) if a <= anchor < b), None) + if pos is None: + raise _unsupported("target segment not found in inline parent") + if len(parts) == 1: + return p0 + 1, p1 - 1 + if pos + 1 < len(parts): + return parts[pos][0], parts[pos + 1][0] + return parts[pos - 1][1], parts[pos][1] + + +def _same_parse(candidate: str, expected: Any) -> bool: + try: + return _same(parse_document(candidate), expected) + except KtavDecodeError: + return False + + +def remove_value_segments(source: str, segments: list[str]) -> str: + """Remove an existing value by decoded literal path segments.""" + original = parse_document(source) + if not segments: + raise _not_found() + if any(not isinstance(segment, str) for segment in segments): + raise KtavEditError("edit path segments must be strings", error="Message") + expected = _expected_without(original, segments) + root, explicit = _build_tree(source) + node, _inline, _root_first = _descend(root, list(segments), explicit) + slot = node.slot + if slot is None: + raise _unsupported("target is a noncontiguous value (dotted-key/reopen intermediate); no lossless single-range removal exists") + if node.inline: + start, end = _inline_range(source, root, list(segments), explicit, slot) + else: + anchor = slot[1] if slot[0] != "block" else slot[2] + start = _line_start(source, anchor) + if start == 0 and source.startswith("\ufeff"): + start = 1 + end = _line_end(source, slot[3] if slot[0] != "block" else slot[3]) + if end == len(source) and not source[start:end].endswith(("\n", "\r")): + if start and source[start - 1] == "\n": + start -= 1 + if start and source[start - 1] == "\r": + start -= 1 + elif start and source[start - 1] == "\r": + start -= 1 + if node.kind in ("object", "array"): + ranges: list[tuple[int, int]] = [] + _collect_multiline_scalar_ranges(node, ranges) + if _has_comment_line(source, start, end, ranges): + raise _unsupported("target contains comment lines; removal cannot preserve them losslessly") + + cut_candidate = source[:start] + source[end:] + if _same_parse(cut_candidate, expected): + return cut_candidate + + replacement = None + ancestors = segments[:-1] + emptied_from = None + for first in range(len(ancestors)): + all_empty = True + for depth in range(first, len(ancestors)): + value = _lookup_value(expected, ancestors[:depth + 1]) + if not isinstance(value, dict) or value: + all_empty = False + break + if all_empty: + emptied_from = first + break + if emptied_from is not None: + m = emptied_from + synthetic, _inl, _first = _descend(root, ancestors[:m + 1], explicit) + if synthetic.slot is None: + container_depth = 0 + for depth in range(len(ancestors)): + n, _, _ = _descend(root, ancestors[:depth + 1], explicit) + if n.slot is not None: + container_depth = depth + 1 + if m >= container_depth: + lead = 0 + if start < end and source[start:start + 2] == "\r\n": + lead = 2 + elif start < end and source[start:start + 1] in ("\r", "\n"): + lead = 1 + line_start = start + lead + line_end = line_start + while line_end < len(source) and source[line_end] in WS_CHARS: + line_end += 1 + indent = source[line_start:line_end] + parts: list[str] = [] + for segment in ancestors[container_depth:]: + if parts: + parts.append(".") + push_key_segment(segment, False, parts) + key = "".join(parts) + replacement = source[start:line_start] + indent + key + ": {}" + if end and source[end - 1] in "\r\n": + eol_start = end - 1 + if source[eol_start] == "\n" and eol_start and source[eol_start - 1] == "\r": + eol_start -= 1 + replacement += source[eol_start:end] + + candidate = source[:start] + (replacement or "") + source[end:] + if _same_parse(candidate, expected): + return candidate + raise _unsupported("semantic mismatch after splice; no lossless patch produced") + diff --git a/python/ktav/_pure/_edit_rename.py b/python/ktav/_pure/_edit_rename.py new file mode 100644 index 0000000..7aac1d8 --- /dev/null +++ b/python/ktav/_pure/_edit_rename.py @@ -0,0 +1,134 @@ +"""Lossless source splice for renaming an existing Object key.""" + +from __future__ import annotations + +from typing import Any + +from ._edit_common import _lookup_value, _not_found, _parse_index, _unsupported, _same +from ._edit_tree import _build_tree, _descend +from ._keys import split_key_path +from ._text import is_inline_whitespace +from .errors import KtavDecodeError, KtavEditError, KtavAlreadyExistsError +from .parser import parse_document +from .writer import push_key_segment + +__all__ = ["rename_key_segments"] + + +def _clone(value: Any) -> Any: + if isinstance(value, dict): + return {key: _clone(child) for key, child in value.items()} + if isinstance(value, list): + return [_clone(child) for child in value] + return value + + +def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: + """Rename the final addressed Object member, preserving source layout.""" + original = parse_document(source) + if not segments: + raise _not_found() + if not isinstance(new_key, str): + raise KtavEditError("new key must be a string", error="Message") + if not new_key: + raise _unsupported("empty key name is not representable") + key, parents = segments[-1], list(segments[:-1]) + parent = _lookup_value(original, parents) + if isinstance(parent, list): + raise _unsupported("rename of an Array element is not supported; only Object keys can be renamed") + if not isinstance(parent, dict) or key not in parent: + raise _not_found() + if key == new_key: + return source + if new_key in parent: + raise KtavAlreadyExistsError("object member already exists", error="EditAlreadyExists") + + root, root_explicit = _build_tree(source) + parent_node, _inline, _root_first = _descend(root, parents, root_explicit) + if parent_node.kind != "object": + raise _unsupported("internal: parent node is not an object") + target = parent_node.child(key) + if target is None: + raise _not_found() + slot = target.slot + if slot is None: + raise _unsupported("target is a noncontiguous value (dotted-key/reopen intermediate); no lossless rename range exists") + if slot[0] not in ("pair", "block") or (slot[0] == "block" and slot[1] is None): + raise _unsupported("rename of an Array element is not supported; only Object keys can be renamed") + colon = slot[1] + # Find the final key segment within its pair, respecting inline + # delimiters, quoted segments, escapes, dotted paths, and a leading BOM. + key_end = colon + while key_end > 0 and is_inline_whitespace(source[key_end - 1]): + key_end -= 1 + start = max(source.rfind("\n", 0, colon), source.rfind("\r", 0, colon)) + 1 + if start == 0 and source.startswith("\ufeff"): + start = 1 + boundary = start + i = start + segment_start = True + while i < key_end: + if segment_start: + while i < key_end and is_inline_whitespace(source[i]): + i += 1 + if i < key_end and source[i] in "\"'`": + quote = source[i] + i += 1 + while i < key_end: + if source[i] == "\\": + i += 2 + elif source[i] == quote: + i += 1 + break + else: + i += 1 + segment_start = False + continue + segment_start = False + if source[i] == "\\": + i += 2 + continue + if source[i] == ".": + boundary = i + 1 + segment_start = True + elif source[i] in ",{[": + boundary = i + 1 + segment_start = True + i += 1 + while boundary < key_end and is_inline_whitespace(source[boundary]): + boundary += 1 + start = boundary + end = key_end + raw_key = source[start:end] + parts = split_key_path(raw_key) + if not parts: + raise _unsupported("internal: key without segments") + last = parts[-1] + seg_start = start + sum(len(part) + 1 for part in parts[:-1]) + first_root_key = not parents and next(iter(parent_node.children), None) == key + encoded: list[str] = [] + push_key_segment(new_key, first_root_key, encoded) + result = source[:seg_start] + "".join(encoded) + source[seg_start + len(last):] + + expected = _clone(original) + expect_parent = expected + for segment in parents: + if isinstance(expect_parent, list): + index = _parse_index(segment) + if index is None: + raise _not_found() + expect_parent = expect_parent[index] + else: + expect_parent = expect_parent[segment] + renamed = {} + for old, value in expect_parent.items(): + renamed[new_key if old == key else old] = value + expect_parent.clear() + expect_parent.update(renamed) + try: + actual = parse_document(result) + except KtavDecodeError as exc: + raise _unsupported(f"patch produced an invalid document: {exc}") from None + if not _same(actual, expected): + raise _unsupported("semantic mismatch after splice; no lossless patch produced") + return result diff --git a/python/ktav/_pure/_edit_splice.py b/python/ktav/_pure/_edit_splice.py index bec65ae..237f7c9 100644 --- a/python/ktav/_pure/_edit_splice.py +++ b/python/ktav/_pure/_edit_splice.py @@ -14,6 +14,7 @@ from ._edit_common import ( _detect_eol, _line_indent, + _line_start, _lookup_value, _not_found, _parse_index, @@ -28,16 +29,66 @@ _PlanCtx, ) from ._edit_layout import _layout_adjust +from ._text import WS_CHARS from ._edit_tree import ( _build_tree, _collect_multiline_scalar_ranges, _descend, _has_comment_line, ) -from .errors import KtavDecodeError +from .errors import KtavDecodeError, KtavEditError from .parser import parse_document -__all__ = ["apply_edit", "edit_value", "edit_value_segments"] +__all__ = ["apply_edit", "edit_value", "edit_value_segments", "insert_value", "insert_value_segments"] + + +def _root_closer_line_start(text: str) -> int | None: + end = len(text) + while end > 0: + start = _line_start(text, end - 1) + stop = start + while stop < len(text) and text[stop] not in "\r\n": + stop += 1 + if text[start:stop].strip(WS_CHARS) == "}": + return start + end = start + return None + + +def _sibling_indent(text: str, node: Any) -> str | None: + children = node.children + if node.kind != "object" or not isinstance(children, dict) or not children: + return None + last = next(reversed(children.values())) + slot = last.slot + if slot is None: + return None + pos = slot[1] if slot[0] in ("pair", "item") else slot[2] + return _line_indent(text, _line_start(text, pos)) + +def _block_encodable(value: Any) -> bool: + if isinstance(value, str): + return "\r" not in value and not any(line.strip(" \t") == "))" for line in value.split("\n")) + if isinstance(value, dict): + return all(_block_encodable(child) for child in value.values()) + if isinstance(value, list): + return all(_block_encodable(child) for child in value) + return True + + +def _insert_pair(key: str, value: Any, indent: str, eol: str, inline: bool) -> str: + """Render one newly inserted object member in block or inline form.""" + from ._edit_encode import _PlanCtx, _plan_value, _inline_body + from .writer import push_key_segment + + parts: list[str] = [] + push_key_segment(key, False, parts) + encoded_key = "".join(parts) + if inline: + return f"{encoded_key}: {_inline_body(value, True)}" + pc = _PlanCtx("pair", False, False, False, indent, eol, False, True) + body, raw = _plan_value(value, pc) + return f"{encoded_key}:{':' if raw else ''} {body}" def apply_edit(source: str, segments: list[str], replacement: Any) -> str: @@ -197,8 +248,173 @@ def edit_value_segments(source: str, segments: list[str], replacement: Any) -> s return _edit_segments_checked(source, list(segments), replacement) + + def _edit_segments_checked(source: str, segments: list[str], replacement: Any) -> str: from .writer import to_value value = to_value(replacement) return apply_edit(source, segments, value) + + +def insert_value(source: str, path: str, value: Any) -> str: + """Insert a new Object member at textual ``path`` (parent.child).""" + from ._edit_common import parse_path + + return insert_value_segments(source, parse_path(path), value) + + +def insert_value_segments(source: str, segments: list[str], value: Any) -> str: + """Losslessly append a new member to an existing Object.""" + from .writer import to_value + from ._edit_common import _line_start, _line_indent, _detect_eol, _parse_index + from ._edit_layout import _requires_block + from .errors import KtavAlreadyExistsError, KtavEditError + + + original = parse_document(source) + if not segments: + raise _not_found() + decoded = list(segments) + if any(not isinstance(s, str) for s in decoded): + raise KtavEditError("edit path segments must be strings", error="Message") + value = to_value(value) + replacement = value + key, parents = decoded[-1], decoded[:-1] + parent = _lookup_value(original, parents) + if isinstance(parent, list): + raise _unsupported("insertion into an Array is not supported; only Object members can be created") + if not isinstance(parent, dict): + raise _not_found() + if key in parent: + raise KtavAlreadyExistsError("object member already exists", error="EditAlreadyExists") + root, root_explicit = _build_tree(source) + node = root + for seg in parents: + node = node.child(seg) if node.kind == "object" else node.item(_parse_index(seg) if _parse_index(seg) is not None else -1) if node.kind == "array" else None + if node is None: + raise _not_found() + eol = _detect_eol(source, len(source)) + slot = node.slot + inline_body: tuple[int, int] | None = None + if slot is not None and slot[0] in ("pair", "item"): + b0, b1 = slot[2], slot[3] + if b0 < b1 and source[b0] == "{" and source[b1 - 1] == "}": + inline_body = (b0, b1) + elif not parents and root.inline: + from ._edit_common import _physical_line_span + line_start = 0 + while line_start < len(source): + _ls, _le, raw_line = _physical_line_span(source, line_start) + line = raw_line.strip(WS_CHARS) + if _ls == 0 and line.startswith("\ufeff"): + line = line[1:] + line = line.strip(WS_CHARS) + if not line or line.startswith("#"): + line_start = _le + (1 if _le < len(source) else 0) + if _le < len(source) and source[_le] == "\r" and _le + 1 < len(source) and source[_le + 1] == "\n": + line_start += 1 + continue + leading = len(raw_line) - len(raw_line.lstrip(WS_CHARS)) + start = _ls + leading + if _ls == 0: + bom_end = start + len(raw_line[leading:]) - len(raw_line[leading:].lstrip("\ufeff")) + start = bom_end + start += len(raw_line[start - _ls:]) - len(raw_line[start - _ls:].lstrip(WS_CHARS)) + if line.startswith("{") and line.endswith("}"): + inline_body = (start, start + len(line)) + break + if inline_body is not None: + a, b = inline_body + eol = _detect_eol(source, b) + inner = source[a + 1 : b - 1] + indent = _line_indent(source, a) + wants_block = _requires_block(replacement, 0) or (isinstance(replacement, str) and ("\n" in replacement or "\r" in replacement)) + force = wants_block and _block_encodable(replacement) + pair = _insert_pair(key, replacement, indent, eol, True) + trimmed = inner.rstrip(WS_CHARS) + if not inner.strip(WS_CHARS): + at, to, inserted = a + 1, b - 1, pair + else: + comma = trimmed.endswith(",") + slash_count = 0 + if comma: + index = len(trimmed) - 2 + while index >= 0 and trimmed[index] == "\\": + slash_count += 1 + index -= 1 + comma_is_structural = comma and slash_count % 2 == 0 + if comma_is_structural: + at, to, inserted = a + 1 + len(trimmed), a + 1 + len(trimmed), " " + pair + "," + else: + at, to, inserted = b - 1, b - 1, ", " + pair + result = source[:at] + inserted + source[to:] + return _verify_insert_result(source, result, decoded, replacement, original, root, at, to, inserted, force) + if not parents and root_explicit: + closer = _root_closer_line_start(source) + if closer is None: + raise _unsupported("internal: explicit root without closer") + indent = _sibling_indent(source, root) + if indent is None: + indent = " " + at = closer + inserted = indent + _insert_pair(key, replacement, indent, _detect_eol(source, at), False) + _detect_eol(source, at) + result = source[:at] + inserted + source[at:] + return _verify_insert_result(source, result, decoded, replacement, original, root, at, at, inserted, False) + if not parents: + at = len(source) + pair = _insert_pair(key, replacement, "", eol, False) + lead = "" if not source or source.endswith(("\n", "\r")) else eol + tail = eol if source and source.endswith(("\n", "\r")) else "" + return _verify_insert_result(source, source + lead + pair + tail, decoded, replacement, original, root, at, at, lead + pair + tail, False) + if slot is None or slot[0] != "block": + raise _unsupported("parent is a noncontiguous value; no lossless insertion point exists") + end = slot[3] + eol = _detect_eol(source, end) + at = _line_start(source, end) + indent = _sibling_indent(source, node) + if indent is None: + indent = _line_indent(source, slot[2]) + " " + pair_key = _insert_pair(key, replacement, indent, eol, False) + inserted = indent + pair_key + eol + result = source[:at] + inserted + source[at:] + return _verify_insert_result(source, result, decoded, replacement, original, root, at, at, inserted, False) + + +def _clone_value(value: Any) -> Any: + if isinstance(value, dict): + return {key: _clone_value(child) for key, child in value.items()} + if isinstance(value, list): + return [_clone_value(child) for child in value] + return value + + +def _verify_insert_result(source: str, result: str, segments: list[str], replacement: Any, original: Any, root: Any, frm: int, to: int, inserted: str, force: bool = False) -> str: + from ._edit_common import _physical_line + from ._edit_layout import _layout_adjust + old_line = _physical_line(source, frm) + new_line = _physical_line(result, frm) + if force or len(new_line) > max(len(old_line), 100): + if _block_encodable(replacement): + result = _layout_adjust(source, result, root, segments[:-1], replacement, (frm, to, len(inserted)), trim_owner_trailing=True) + try: + actual_doc = parse_document(result) + except KtavDecodeError as exc: + raise _unsupported(f"patch produced an invalid document: {exc}") from None + try: + expected = _clone_value(original) + parent = expected + for seg in segments[:-1]: + if isinstance(parent, dict): + parent = parent[seg] + else: + idx = _parse_index(seg) + if idx is None: + raise KeyError(seg) + parent = parent[idx] + parent[segments[-1]] = replacement + except (KeyError, IndexError, TypeError): + raise _unsupported("semantic mismatch after splice; no lossless patch produced") from None + if not _same(actual_doc, expected): + raise _unsupported("semantic mismatch after splice; no lossless patch produced") + return result diff --git a/python/ktav/_pure/errors.py b/python/ktav/_pure/errors.py index 4ef44fb..d2e22ab 100644 --- a/python/ktav/_pure/errors.py +++ b/python/ktav/_pure/errors.py @@ -97,6 +97,10 @@ class KtavNotFoundError(KtavEditError): """The path does not address an existing value.""" +class KtavAlreadyExistsError(KtavEditError): + """The target Object key already exists; edits never overwrite keys.""" + + class KtavUnsupportedError(KtavEditError): """No lossless single-range splice exists for this edit.""" diff --git a/python/ktav/pure.py b/python/ktav/pure.py index 9197da4..a3569be 100644 --- a/python/ktav/pure.py +++ b/python/ktav/pure.py @@ -17,6 +17,7 @@ from __future__ import annotations from ._pure import ( + KtavAlreadyExistsError, KtavDecodeError, KtavEditError, KtavEncodeError, @@ -32,6 +33,12 @@ dumps_force_strings, edit_value, edit_value_segments, + insert_value, + insert_value_segments, + remove_value, + remove_value_segments, + rename_key, + rename_key_segments, emit_canonical, format, load, @@ -40,6 +47,7 @@ ) __all__ = [ + "KtavAlreadyExistsError", "KtavDecodeError", "KtavEditError", "KtavEncodeError", @@ -55,6 +63,12 @@ "dumps_force_strings", "edit_value", "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", "emit_canonical", "format", "load", diff --git a/root-docs/CHANGELOG/source-edit09/body-1.md b/root-docs/CHANGELOG/source-edit09/body-1.md index 5199dfa..d6fa6d9 100644 --- a/root-docs/CHANGELOG/source-edit09/body-1.md +++ b/root-docs/CHANGELOG/source-edit09/body-1.md @@ -11,6 +11,16 @@ byte offset into the path), `KtavNotFoundError`, and `KtavUnsupportedError`; the same ten-field envelope, `error` is one of `EditInvalidPath` / `EditNotFound` / `EditUnsupported`. +- `ktav.insert_value(source, path, value)` and + `ktav.insert_value_segments(source, segments, value)` add a new Object + member without overwriting an existing key. `ktav.remove_value(source, path)` + and `ktav.remove_value_segments(source, segments)` remove an existing + value (array elements shift; empty parent Objects remain). +- `ktav.rename_key(source, path, new_key)` and + `ktav.rename_key_segments(source, segments, new_key)` rename an Object + member in place; `new_key` is one literal key, not a dotted path. + All six APIs use lossless source splices, are available in native and + pure-Python backends, and reject unsupported structural cases explicitly. - Spec 0.9 corpus: the conformance runner now executes the `versions/0.9/tests` fixtures (162 invalid — 72 new `diagnostics09` EOF/malformed-UTF-8 diagnostics), validates `expected_location` @@ -40,6 +50,16 @@ путь), `KtavNotFoundError` и `KtavUnsupportedError`; тот же десятипольный конверт, `error` — один из `EditInvalidPath` / `EditNotFound` / `EditUnsupported`. +- `ktav.insert_value(source, path, value)` и + `ktav.insert_value_segments(source, segments, value)` добавляют новый + член Object без перезаписи существующего ключа. `ktav.remove_value(source, path)` + и `ktav.remove_value_segments(source, segments)` удаляют существующее + значение (элементы массива сдвигаются; пустые родительские Object остаются). +- `ktav.rename_key(source, path, new_key)` и + `ktav.rename_key_segments(source, segments, new_key)` переименовывают член Object + на месте; `new_key` — одно буквальное имя, а не dotted-путь. + Все шесть API выполняют lossless-splice исходника, доступны в native и + pure-Python backend и явно отклоняют неподдерживаемые структурные случаи. - Корпус spec 0.9: conformance runner исполняет fixtures из `versions/0.9/tests` (162 invalid — 72 новых диагностики `diagnostics09` EOF/битый UTF-8), проверяет метаданные @@ -67,6 +87,15 @@ UTF-8 字节偏移十进制串)、`KtavNotFoundError` 与 `KtavUnsupportedError`;信封仍是同样的十字段,`error` 取 `EditInvalidPath` / `EditNotFound` / `EditUnsupported` 之一。 +- `ktav.insert_value(source, path, value)` 与 + `ktav.insert_value_segments(source, segments, value)` 新增 Object 成员, + 不覆盖已有键。`ktav.remove_value(source, path)` 与 + `ktav.remove_value_segments(source, segments)` 删除已有值(数组元素前移, + 空的父 Object 保留)。 +- `ktav.rename_key(source, path, new_key)` 与 + `ktav.rename_key_segments(source, segments, new_key)` 原位重命名 Object 成员; + `new_key` 是一个字面键名,而非点分路径。六个 API 均通过无损源文本 splice + 实现,native 与 pure-Python 后端均可用,并会明确拒绝不支持的结构情况。 - spec 0.9 语料:conformance 运行器现在执行 `versions/0.9/tests` 的 fixture(162 个 invalid —— 其中 72 个是新的 `diagnostics09` EOF/坏 UTF-8 诊断),校验 `expected_location` 元数据(line、 diff --git a/root-docs/README/source-edit/body-1.md b/root-docs/README/source-edit/body-1.md index d5f12a3..45458d1 100644 --- a/root-docs/README/source-edit/body-1.md +++ b/root-docs/README/source-edit/body-1.md @@ -21,19 +21,6 @@ an Array. Only replacement of an existing value is supported; missing keys, out-of-bounds indexes and descent through a scalar raise `KtavNotFoundError`. -The edit is a source splice, not a parse→mutate→serialize of the whole -document: comments, key spelling, order, whitespace, BOM and -CR/LF/CRLF outside the effective change are preserved byte-for-byte, -dotted/inline/block forms are kept, and an equal replacement is a -byte-identical no-op — even for noncanonical spelling. Every -successful patch is re-parsed and semantically verified. When no -lossless patch exists (noncontiguous dotted/reopened targets, internal -comments under structural replacement, an unrepresentable value in the -target context) the call raises `KtavUnsupportedError` instead of -guessing. Long or multiline replacements unfold only the touched -inline wrappers; untouched nested branches stay inline. Both the -native and the pure backend implement the identical semantics. - >>>>> lang=ru ### Правка исходника — `edit_value` / `edit_value_segments` @@ -57,19 +44,6 @@ UTF-8 byte offset проблемного места. `edit_value_segments` пр ключ, выход за границы массива и спуск сквозь скаляр дают `KtavNotFoundError`. -Правка — это splice исходника, а не parse→mutate→serialize всего -документа: комментарии, написание ключей, порядок, пробелы, BOM и -CR/LF/CRLF вне эффективного изменения сохраняются побайтово, -dotted/inline/block-формы держатся, а совпадающая замена — -byte-identical no-op, даже при неканоничном написании. Каждый -успешный патч перепарсится и семантически проверяется. Когда lossless -патча не существует (noncontiguous dotted/reopen-цели, внутренние -комментарии при структурной замене, непредставимое в контексте цели -значение), вызов бросает `KtavUnsupportedError`, а не угадывает. Длинные -и многострочные замены раскрывают только затронутые inline-обёртки; -нетронутые ветки остаются inline. Native и pure backend реализуют -идентичную семантику. - >>>>> lang=zh ### 源文本编辑 —— `edit_value` / `edit_value_segments` @@ -91,13 +65,3 @@ literal 点号,`\\` 表示 literal 反斜杠;任何其他转义都会抛出 既有值:缺失键、越界索引与穿过标量的下降都会抛出 `KtavNotFoundError`。 -编辑是对源文本的 splice,而不是整个文档的 -parse→mutate→serialize:有效修改之外的注释、键拼写、顺序、空白、 -BOM 与 CR/LF/CRLF 按字节保留,dotted/inline/block 形式得以维持, -相同替换是字节级 no-op —— 即使拼写非规范。每个成功的补丁都会 -重新解析并做语义校验。当不存在无损补丁时(noncontiguous 的 -dotted/reopen 目标、结构替换下的内部注释、或在目标上下文中无法 -无损表示的值),调用抛出 `KtavUnsupportedError` 而不是猜测。长或多行 -替换只展开被触及的 inline 包装层;未触及的嵌套分支保持 inline。 -native 与 pure backend 实现完全一致的语义。 - diff --git a/root-docs/README/source-edit/body-2.md b/root-docs/README/source-edit/body-2.md new file mode 100644 index 0000000..24c57b9 --- /dev/null +++ b/root-docs/README/source-edit/body-2.md @@ -0,0 +1,75 @@ +>>>>> lang=en +The edit is a source splice, not a parse→mutate→serialize of the whole +document: comments, key spelling, order, whitespace, BOM and +CR/LF/CRLF outside the effective change are preserved byte-for-byte, +dotted/inline/block forms are kept, and an equal replacement is a +byte-identical no-op — even for noncanonical spelling. Every +successful patch is re-parsed and semantically verified. When no +lossless patch exists (noncontiguous dotted/reopened targets, internal +comments under structural replacement, an unrepresentable value in the +target context) the call raises `KtavUnsupportedError` instead of +guessing. Long or multiline replacements unfold only the touched +inline wrappers; untouched nested branches stay inline. Both the +native and the pure backend implement the identical semantics. + +### Structural source edits — insert, remove, rename + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# Literal segments avoid path escaping: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + +>>>>> lang=ru +Правка — это splice исходника, а не parse→mutate→serialize всего +документа: комментарии, написание ключей, порядок, пробелы, BOM и +CR/LF/CRLF вне эффективного изменения сохраняются побайтово, +dotted/inline/block-формы держатся, а совпадающая замена — +byte-identical no-op, даже при неканоничном написании. Каждый +успешный патч перепарсится и семантически проверяется. Когда lossless +патча не существует (noncontiguous dotted/reopen-цели, внутренние +комментарии при структурной замене, непредставимое в контексте цели +значение), вызов бросает `KtavUnsupportedError`, а не угадывает. Длинные +и многострочные замены раскрывают только затронутые inline-обёртки; +нетронутые ветки остаются inline. Native и pure backend реализуют +идентичную семантику. + +### Структурные правки исходника — вставка, удаление, переименование + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# Literal-сегменты не требуют экранирования пути: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + +>>>>> lang=zh +编辑是对源文本的 splice,而不是整个文档的 +parse→mutate→serialize:有效修改之外的注释、键拼写、顺序、空白、 +BOM 与 CR/LF/CRLF 按字节保留,dotted/inline/block 形式得以维持, +相同替换是字节级 no-op —— 即使拼写非规范。每个成功的补丁都会 +重新解析并做语义校验。当不存在无损补丁时(noncontiguous 的 +dotted/reopen 目标、结构替换下的内部注释、或在目标上下文中无法 +无损表示的值),调用抛出 `KtavUnsupportedError` 而不是猜测。长或多行 +替换只展开被触及的 inline 包装层;未触及的嵌套分支保持 inline。 +native 与 pure backend 实现完全一致的语义。 + +### 结构化源文本编辑 —— 插入、删除与重命名 + +```python +inserted = ktav.insert_value(source, "service.timeout", 30) +removed = ktav.remove_value(inserted, "service.old") +renamed = ktav.rename_key(removed, "service.timeout", "deadline") + +# literal 段无需路径转义: +inserted = ktav.insert_value_segments(source, ["a.b", "x\\y"], "v") +renamed = ktav.rename_key_segments(source, ["a.b", "x\\y"], "z.w") +``` + diff --git a/root-docs/README/source-edit/body-3.md b/root-docs/README/source-edit/body-3.md new file mode 100644 index 0000000..b84dd48 --- /dev/null +++ b/root-docs/README/source-edit/body-3.md @@ -0,0 +1,57 @@ +>>>>> lang=en +`insert_value` and `remove_value` take the same textual paths as +`edit_value`; the `_segments` forms take already-decoded literal +segments. Insert's final segment names a new Object key: its parent must +exist and be an Object, and an existing key is never overwritten. Array +insertion is unsupported. Remove accepts an existing value, including +an array element (later elements shift); emptied parent Objects remain +rather than being pruned. Rename addresses an Object member and takes +`new_key` as one literal name, not a path; dots and backslashes there +are ordinary characters. The member's position and value stay put, and +a name collision is rejected. + +These operations splice source text rather than serializing the whole +document. Existing layout and unrelated text are preserved whenever a +lossless patch is possible; unsupported structural cases fail explicitly +instead of guessing. Empty segment lists and missing targets are errors. +The insert, remove and rename APIs are available with the native +backend. The pure-Python backend implements insert, remove and rename (including +`insert_value_segments`); their current readiness and conformance are pending CI +verification. + +>>>>> lang=ru +`insert_value` и `remove_value` принимают текстовые пути как +`edit_value`; варианты `_segments` принимают уже декодированные +literal-сегменты. Последний сегмент вставки задаёт новый ключ Object: +родитель должен существовать и быть Object, существующий ключ никогда +не перезаписывается. Вставка в Array не поддерживается. Удаление +принимает существующее значение, включая элемент массива (последующие +элементы сдвигаются); опустевшие родительские Object сохраняются. +Переименование адресует член Object, а `new_key` — одно буквальное имя, +не путь: точки и обратные косые черты в нём являются обычными символами. +Позиция и значение члена сохраняются; конфликт имени отклоняется. + +Эти операции выполняют splice исходника, а не сериализуют весь документ. +Существующая раскладка и посторонний текст сохраняются, если возможна +lossless-правка; неподдерживаемые структурные случаи явно завершаются +ошибкой вместо догадок. Пустой список сегментов и отсутствующая цель — +ошибки. API вставки, удаления и переименования доступны с native +backend. Pure-Python backend реализует вставку, удаление и переименование +(включая `insert_value_segments`); их текущая готовность и соответствие +ожидают проверки CI. + +>>>>> lang=zh +`insert_value` 和 `remove_value` 接受与 `edit_value` 相同的文本路径; +`_segments` 形式接受已解码的 literal 段。插入路径的最后一段是新 +Object 键:父级必须存在且为 Object,已有键绝不会被覆盖。不支持向 +Array 插入。删除接受既有值,包括数组元素(后续元素会前移);变空 +的父 Object 不会被裁掉。重命名定位 Object 成员,`new_key` 是单个 +literal 名称而非路径,其中的点号和反斜杠都是普通字符。成员位置与 +值保持不变;名称冲突会被拒绝。 + +这些操作通过 splice 修改源文本,而不是序列化整个文档。只要存在 +无损补丁,就保留已有布局及无关文本;不支持的结构情形会明确失败, +不会猜测。空段列表和不存在的目标均为错误。插入、删除与重命名 API +可在 native backend 中使用。Pure-Python backend 已实现插入、删除与重命名(包括 +`insert_value_segments`);其当前就绪度与符合预期情况仍待 CI 验证。 + diff --git a/root-docs/README/source-edit/meta.js b/root-docs/README/source-edit/meta.js index eb86416..6f4ff20 100644 --- a/root-docs/README/source-edit/meta.js +++ b/root-docs/README/source-edit/meta.js @@ -3,5 +3,5 @@ export default { "number": null, "level": null, "title": null, - "bodyParts": 1 + "bodyParts": 3 } diff --git a/src/lib.rs b/src/lib.rs index 4c8b2dd..e2826e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ create_exception!(_core, KtavEncodeError, KtavError); create_exception!(_core, KtavEditError, KtavError); create_exception!(_core, KtavInvalidPathError, KtavEditError); create_exception!(_core, KtavNotFoundError, KtavEditError); +create_exception!(_core, KtavAlreadyExistsError, KtavEditError); create_exception!(_core, KtavUnsupportedError, KtavEditError); /// Shorthand for handing the edit exception *class object* for an @@ -53,6 +54,7 @@ fn edit_error_class<'py>(py: Python<'py>, err: &ktav::edit::EditError) -> Bound< py.get_type::().into_any() } ktav::edit::EditError::NotFound => py.get_type::().into_any(), + ktav::edit::EditError::AlreadyExists => py.get_type::().into_any(), ktav::edit::EditError::Unsupported { .. } => { py.get_type::().into_any() } @@ -77,6 +79,7 @@ fn structured_edit_error(py: Python<'_>, err: &ktav::edit::EditError) -> PyErr { ("EditInvalidPath", Some(offset.to_string())) } ktav::edit::EditError::NotFound => ("EditNotFound", None), + ktav::edit::EditError::AlreadyExists => ("EditAlreadyExists", None), ktav::edit::EditError::Unsupported { reason } => ("EditUnsupported", Some(reason.clone())), _ => ("EditUnsupported", Some(err.to_string())), }; @@ -599,6 +602,70 @@ fn edit_value_segments( .map_err(|e| structured_edit_error(py, &e)) } +/// Insert an Object member at a textual path without overwriting existing keys. +#[pyfunction] +#[pyo3(text_signature = "(source, path, value, /)")] +fn insert_value( + py: Python<'_>, + source: &str, + path: &str, + replacement: &Bound<'_, PyAny>, +) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + let value = py_to_value(replacement) + .map_err(|e| structured_error(py, &encode_error_class(py), &e, ""))?; + ktav::edit::insert_value(source, path, &value).map_err(|e| structured_edit_error(py, &e)) +} + +#[pyfunction] +#[pyo3(text_signature = "(source, segments, value, /)")] +fn insert_value_segments( + py: Python<'_>, + source: &str, + segments: Vec, + replacement: &Bound<'_, PyAny>, +) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + let value = py_to_value(replacement) + .map_err(|e| structured_error(py, &encode_error_class(py), &e, ""))?; + ktav::edit::insert_value_segments(source, &segments, &value) + .map_err(|e| structured_edit_error(py, &e)) +} + +#[pyfunction] +#[pyo3(text_signature = "(source, path, /)")] +fn remove_value(py: Python<'_>, source: &str, path: &str) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + ktav::edit::remove_value(source, path).map_err(|e| structured_edit_error(py, &e)) +} + +#[pyfunction] +#[pyo3(text_signature = "(source, segments, /)")] +fn remove_value_segments(py: Python<'_>, source: &str, segments: Vec) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + ktav::edit::remove_value_segments(source, &segments).map_err(|e| structured_edit_error(py, &e)) +} + +#[pyfunction] +#[pyo3(text_signature = "(source, path, new_key, /)")] +fn rename_key(py: Python<'_>, source: &str, path: &str, new_key: &str) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + ktav::edit::rename_key(source, path, new_key).map_err(|e| structured_edit_error(py, &e)) +} + +#[pyfunction] +#[pyo3(text_signature = "(source, segments, new_key, /)")] +fn rename_key_segments( + py: Python<'_>, + source: &str, + segments: Vec, + new_key: &str, +) -> PyResult { + ktav::parse(source).map_err(|e| structured_error(py, &decode_error_class(py), &e, source))?; + ktav::edit::rename_key_segments(source, &segments, new_key) + .map_err(|e| structured_edit_error(py, &e)) +} + #[pymodule] fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add("__version__", env!("CARGO_PKG_VERSION"))?; @@ -613,6 +680,12 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(dumps_force_strings, m)?)?; m.add_function(wrap_pyfunction!(edit_value, m)?)?; m.add_function(wrap_pyfunction!(edit_value_segments, m)?)?; + m.add_function(wrap_pyfunction!(insert_value, m)?)?; + m.add_function(wrap_pyfunction!(insert_value_segments, m)?)?; + m.add_function(wrap_pyfunction!(remove_value, m)?)?; + m.add_function(wrap_pyfunction!(remove_value_segments, m)?)?; + m.add_function(wrap_pyfunction!(rename_key, m)?)?; + m.add_function(wrap_pyfunction!(rename_key_segments, m)?)?; let py = m.py(); m.add("KtavError", py.get_type::())?; @@ -624,6 +697,10 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { py.get_type::(), )?; m.add("KtavNotFoundError", py.get_type::())?; + m.add( + "KtavAlreadyExistsError", + py.get_type::(), + )?; m.add( "KtavUnsupportedError", py.get_type::(), diff --git a/tests/edit/test_insert_parameterized_regressions.py b/tests/edit/test_insert_parameterized_regressions.py new file mode 100644 index 0000000..ed4a65b --- /dev/null +++ b/tests/edit/test_insert_parameterized_regressions.py @@ -0,0 +1,46 @@ +"""Independent parameterized insert regressions based on Rust source-mutate09.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "path", "segments", "expected", "semantic"), + [ + ("a: 1\n", "b", ["b"], "a: 1\nb: 2\n", {"a": 1, "b": 2}), + ("a: {x: 1}\n", "a.y", ["a", "y"], "a: {x: 1, y: 2}\n", {"a": {"x": 1, "y": 2}}), + ("{x: 1,}\n", "y", ["y"], "{x: 1, y: 2,}\n", {"x": 1, "y": 2}), + ("a: {x: 1, }\n", "a.y", ["a", "y"], "a: {x: 1, y: 2, }\n", {"a": {"x": 1, "y": 2}}), + (r"{x: p\,q\\r, }", "y", ["y"], r"{x: p\,q\\r, y: 2, }", {"x": "p,q\\r", "y": 2}), + ("\ufeffa: 1\r\n", "b", ["b"], "\ufeffa: 1\r\nb: 2\r\n", {"a": 1, "b": 2}), + ("a: 1\nb: 2", "c", ["c"], "a: 1\nb: 2\nc: 2", {"a": 1, "b": 2, "c": 2}), + ], +) +def test_insert_exact_bytes_and_semantics( + api: Any, source: str, path: str, segments: list[str], expected: str, semantic: dict[str, Any] +) -> None: + by_path = api.insert_value(source, path, 2) + by_segments = api.insert_value_segments(source, segments, 2) + assert by_path.encode("utf-8") == expected.encode("utf-8") + assert by_segments.encode("utf-8") == expected.encode("utf-8") + assert api.loads(by_path) == semantic + assert api.loads(by_segments) == semantic + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "path", "code"), + [("a: 1\n", "a", "EditAlreadyExists"), ("a: 1\n", "b.c", "EditNotFound"), ("a: 1\n", "a..b", "EditInvalidPath")], +) +def test_insert_error_contract(api: Any, source: str, path: str, code: str) -> None: + with pytest.raises(api.KtavEditError) as caught: + api.insert_value(source, path, 2) + assert caught.value.error == code diff --git a/tests/edit/test_insert_remaining_regressions.py b/tests/edit/test_insert_remaining_regressions.py new file mode 100644 index 0000000..9aee783 --- /dev/null +++ b/tests/edit/test_insert_remaining_regressions.py @@ -0,0 +1,131 @@ +"""Insert edge-case regressions cross-checked against Rust source-mutate09.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() + + +@pytest.mark.parametrize("api", APIS) +def test_insert_source_parse_error_precedes_invalid_path_segments_and_value(api: Any) -> None: + broken = "a: {" + with pytest.raises(api.KtavDecodeError) as caught: + api.insert_value(broken, "a..b", object()) + assert caught.value.error == "UnclosedCompound" + with pytest.raises(api.KtavDecodeError) as caught: + api.insert_value_segments(broken, [object()], object()) + assert caught.value.error == "UnclosedCompound" + + +@pytest.mark.parametrize("api", APIS) +def test_insert_parse_error_offsets_count_utf8_bytes(api: Any) -> None: + broken = "## café\na: {" + with pytest.raises(api.KtavDecodeError) as caught: + api.insert_value(broken, "a.x", 1) + error = caught.value + assert error.error == "UnclosedCompound" + assert error.span == { + "start": len("## café\na: ".encode("utf-8")), + "end": len(broken.encode("utf-8")), + } + + +@pytest.mark.parametrize("api", APIS) +def test_insert_invalid_path_reports_utf8_byte_offset(api: Any) -> None: + with pytest.raises(api.KtavInvalidPathError) as caught: + api.insert_value("a: 1\n", "é..x", 1) + # parse_path reports the second dot's UTF-8 byte offset: é is two bytes. + assert caught.value.body == "3" + + +@pytest.mark.parametrize("api", APIS) +def test_insert_nbsp_whitespace_cases(api: Any) -> None: + # NBSP is accepted whitespace; inline insertion preserves it after the value. + source = "{\u00a0}" + actual = api.insert_value(source, "a", 1) + assert actual == "{a: 1}" + assert api.loads(actual) == {"a": 1} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_structural_trailing_comma_before_nbsp(api: Any) -> None: + source = "{a: 1,\u00a0}" + expected = "{a: 1, b: 2,\u00a0}" + actual = api.insert_value(source, "b", 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(source) == {"a": 1} + assert api.loads(actual) == {"a": 1, "b": 2} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_empty_explicit_and_nested_multiline_blocks(api: Any) -> None: + assert api.insert_value("{\n}\n", "a", 1) == "{\n a: 1\n}\n" + source = "outer: {\n inner: {\n }\n}\n" + expected = "outer: {\n inner: {\n y: 2\n }\n}\n" + actual = api.insert_value_segments(source, ["outer", "inner", "y"], 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"outer": {"inner": {"y": 2}}} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_array_parent_reports_unsupported(api: Any) -> None: + with pytest.raises(api.KtavEditError) as caught: + api.insert_value_segments("items: [1, 2]\n", ["items", "new"], 3) + assert caught.value.error == "EditUnsupported" + + +@pytest.mark.parametrize("api", APIS) +def test_insert_existing_key_reports_already_exists(api: Any) -> None: + with pytest.raises(api.KtavEditError) as caught: + api.insert_value_segments("a: 1\n", ["a"], 2) + assert caught.value.error == "EditAlreadyExists" + + +@pytest.mark.parametrize("api", APIS) +def test_insert_noncontiguous_parent_is_unsupported(api: Any) -> None: + source = "a.x: 1\na.y: 2\n" + with pytest.raises(api.KtavEditError) as caught: + api.insert_value_segments(source, ["a", "z"], 3) + assert caught.value.error == "EditUnsupported" + + +@pytest.mark.parametrize("api", APIS) +def test_insert_long_inline_object_expands_for_width(api: Any) -> None: + value = "x" * 100 + source = "items: { old: 1, keep: [1, 2] }\n" + actual = api.insert_value_segments(source, ["items", "new"], value) + assert actual == f"items: {{\n old: 1\n keep: [1, 2]\n new: {value}\n}}\n" + assert api.loads(actual) == {"items": {"old": 1, "keep": [1, 2], "new": value}} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_multiline_value_uses_block_form(api: Any) -> None: + source = "items: { old: 1, keep: [1, 2] }\n" + value = "first\nsecond" + actual = api.insert_value_segments(source, ["items", "new"], value) + assert actual == "items: {\n old: 1\n keep: [1, 2]\n new: ((\nfirst\nsecond\n ))\n}\n" + assert api.loads(actual) == {"items": {"old": 1, "keep": [1, 2], "new": value}} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_inline_root_after_leading_comment_expands_with_trailing_space(api: Any) -> None: + source = "## leading comment\n{ x: 1 } \n" + value = "first\nsecond" + actual = api.insert_value(source, "y", value) + expected = "## leading comment\n{\n x: 1\n y: ((\nfirst\nsecond\n ))\n} \n" + assert actual == expected + assert api.loads(actual) == {"x": 1, "y": value} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_leading_comments_braces_and_whitespace(api: Any) -> None: + source = "## { leading }\n { x: 1, } \n## tail\n" + expected = "## { leading }\n { x: 1, y: 2, } \n## tail\n" + actual = api.insert_value(source, "y", 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"x": 1, "y": 2} diff --git a/tests/edit/test_insert_semantic_regressions.py b/tests/edit/test_insert_semantic_regressions.py new file mode 100644 index 0000000..3159e96 --- /dev/null +++ b/tests/edit/test_insert_semantic_regressions.py @@ -0,0 +1,85 @@ +"""Exact-byte and independent semantic insert regressions (Rust source-mutate09 oracle).""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "value", "expected", "semantic"), + [ + ("{a: 1}\n", ["s"], "123", r"{a: 1, s: \u003123}" + "\n", {"a": 1, "s": "123"}), + ("{a: 1}\n", ["s"], "p,q", r"{a: 1, s: p\,q}" + "\n", {"a": 1, "s": "p,q"}), + ( + "{a: 1}\n", + ["s"], + "back\\slash\nline\r", + r"{a: 1, s: back\\slash\nline\r}" + "\n", + {"a": 1, "s": "back\\slash\nline\r"}, + ), + (r"{x: p\,q\,}" + "\n", ["y"], 2, r"{x: p\,q\,, y: 2}" + "\n", {"x": "p,q,", "y": 2}), + ('"a.b": {x: 1}\n', ["a.b", "y"], 2, '"a.b": {x: 1, y: 2}\n', {"a.b": {"x": 1, "y": 2}}), + ("{a: 1,}\n", ["b"], 2, "{a: 1, b: 2,}\n", {"a": 1, "b": 2}), + ("a: 1\n", ["", "b"], 2, "EditNotFound", None), + ("## café\na: 1\n", ["b"], 2, "## café\na: 1\nb: 2\n", {"a": 1, "b": 2}), + ("a: 1\n", ["big"], 9223372036854775807, "a: 1\nbig: 9223372036854775807\n", {"a": 1, "big": 9223372036854775807}), + ("", ["a", "b"], 1, "EditNotFound", None), + ("a: {}\n", ["a", "b"], 1, "a: {b: 1}\n", {"a": {"b": 1}}), + ("", ["a"], 1, "a: 1", {"a": 1}), + ("a: 1\n", ["a"], 2, "EditAlreadyExists", None), + ("a: 1\n", ["missing", "child"], 2, "EditNotFound", None), + ("arr: [1, 2]\n", ["arr", "2"], 3, "EditUnsupported", None), + ("a: 1\n", [], 2, "EditNotFound", None), + ("", ["a", "b"], 1, "EditNotFound", None), + ], +) +def test_insert_exact_and_semantic_oracles(api: Any, source: str, segments: list[str], value: Any, expected: Any, semantic: Any) -> None: + if semantic is None: + with pytest.raises(api.KtavEditError) as caught: + api.insert_value_segments(source, segments, value) + assert caught.value.error == expected + return + actual = api.insert_value_segments(source, segments, value) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +def test_insert_root_comments_preserved_pure(api: Any) -> None: + source = "## { lead }\n{a: 1}\n## { tail }\n" + actual = api.insert_value_segments(source, ["b"], 2) + assert actual.encode("utf-8") == "## { lead }\n{a: 1, b: 2}\n## { tail }\n".encode("utf-8") + assert api.loads(actual) == {"a": 1, "b": 2} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_root_leading_comment_pure(api: Any) -> None: + source = "## only comment\n" + actual = api.insert_value_segments(source, ["b"], 2) + assert actual.encode("utf-8") == "## only comment\nb: 2\n".encode("utf-8") + assert api.loads(actual) == {"b": 2} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_quoted_dotted_key(api: Any) -> None: + source = '"a.b": {x: 1}\n' + expected = '"a.b": {x: 1, y: 2}\n' + actual = api.insert_value_segments(source, ["a.b", "y"], 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"a.b": {"x": 1, "y": 2}} + + +@pytest.mark.parametrize("api", APIS) +def test_source_first_insert_keeps_prefix_and_appends(api: Any) -> None: + source = "## prefix\na: 1\n" + expected = "## prefix\na: 1\nb: 2\n" + actual = api.insert_value(source, "b", 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"a": 1, "b": 2} diff --git a/tests/edit/test_mutation_source_first_regressions.py b/tests/edit/test_mutation_source_first_regressions.py new file mode 100644 index 0000000..42cd944 --- /dev/null +++ b/tests/edit/test_mutation_source_first_regressions.py @@ -0,0 +1,85 @@ +"""Source-first validation precedence for all public mutation APIs.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() +BROKEN = "a: {" + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("name", "args"), + [ + ("insert_value", (BROKEN, "a..b", None)), + ("insert_value_segments", (BROKEN, None, None)), + ("remove_value", (BROKEN, "a..b")), + ("remove_value_segments", (BROKEN, None)), + ("rename_key", (BROKEN, "a..b", None)), + ("rename_key_segments", (BROKEN, None, None)), + ("insert_value", (BROKEN, None, None)), + ("remove_value", (BROKEN, None)), + ("rename_key", (BROKEN, None, None)), + ("insert_value_segments", (BROKEN, [None], None)), + ("remove_value_segments", (BROKEN, [None])), + ("rename_key_segments", (BROKEN, [None], "b")), + ("rename_key_segments", (BROKEN, ["a"], None)), + ], +) +def test_malformed_source_precedes_mutation_argument_validation( + api: Any, name: str, args: tuple[Any, ...] +) -> None: + with pytest.raises(api.KtavDecodeError) as caught: + getattr(api, name)(*args) + assert caught.value.error == "UnclosedCompound" + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("name", "args"), + [ + ("insert_value_segments", ("a: 1\n", [None], 2)), + ("remove_value_segments", ("a: 1\n", [None])), + ("rename_key_segments", ("a: 1\n", [None], "b")), + ("rename_key", ("a: 1\n", "a", None)), + ("rename_key_segments", ("a: 1\n", ["a"], None)), + ], +) +def test_valid_source_reaches_segment_and_new_key_validation( + api: Any, name: str, args: tuple[Any, ...] +) -> None: + with pytest.raises((api.KtavEditError, TypeError, ValueError)): + getattr(api, name)(*args) + + +class RaisingIterator: + def __iter__(self): + raise RuntimeError("segments iterated") + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("name", "args"), + [ + ("insert_value_segments", (BROKEN, 2)), + ("remove_value_segments", (BROKEN,)), + ("rename_key_segments", (BROKEN, "new")), + ], +) +def test_malformed_source_precedes_segments_iterator( + api: Any, name: str, args: tuple[Any, ...] +) -> None: + with pytest.raises(api.KtavDecodeError) as caught: + getattr(api, name)(args[0], RaisingIterator(), *args[1:]) + assert caught.value.error == "UnclosedCompound" + + +@pytest.mark.parametrize("api", APIS) +def test_valid_source_does_iterate_segments(api: Any) -> None: + with pytest.raises(RuntimeError, match="segments iterated"): + api.remove_value_segments("a: 1\n", RaisingIterator()) diff --git a/tests/edit/test_mutation_source_geometry_regressions.py b/tests/edit/test_mutation_source_geometry_regressions.py new file mode 100644 index 0000000..864f1ad --- /dev/null +++ b/tests/edit/test_mutation_source_geometry_regressions.py @@ -0,0 +1,113 @@ +"""Independent source-mutation geometry regressions, matching Rust source-mutate09.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "expected", "semantic"), + [ + # KTAV whitespace before explicit closer; dotted key and closer bytes survive. + ("a: {\n\u00a0x: 1\n\u00a0}\n", ["a", "x"], "a: {\n\u00a0}\n", {"a": {}}), + # Mixed EOLs: nested target is removed without normalizing the LF tail. + ("a: {\r\n b: {\r\n x: 1\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", ["a", "b", "x"], "a: {\r\n b: {\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", {"a": {"b": {}}, "keep": 2, "tail": 3}), + # Deep dotted deletion materializes only the emptied ancestor chain. + ("a.b.c: 1\n", ["a", "b", "c"], "a.b: {}\n", {"a": {"b": {}}}), + ("keep: 0\r\na.b.c: 1", ["a", "b", "c"], "keep: 0\r\na.b: {}", {"keep": 0, "a": {"b": {}}}), + # Dotted deletion in an NBSP-indented nested block materializes ancestors. + ("outer: {\n\u00a0a.b.c: 1\n}\n", ["outer", "a", "b", "c"], "outer: {\n\u00a0a.b: {}\n}\n", {"outer": {"a": {"b": {}}}}), + # Deep dotted deletion materializes through CRLF EOF without adding a final EOL. + ("a.b.c: 1\r\n", ["a", "b", "c"], "a.b: {}\r\n", {"a": {"b": {}}}), + ("items: [\r\n {\r\n a.b: 1\r\n }\r\n]\r\n", ["items", "0", "a", "b"], "items: [\r\n {\r\n a: {}\r\n }\r\n]\r\n", {"items": [{"a": {}}]}), + # Removing final line without EOL keeps EOF unterminated. + ("keep: 1\nlast: 2", ["last"], "keep: 1", {"keep": 1}), + # Removing an earlier final-line target preserves the last line and its EOL state. + ("first: 1\nlast: 2", ["first"], "last: 2", {"last": 2}), + ], +) +def test_remove_source_geometry_exact_bytes_and_semantics( + api: Any, source: str, segments: list[str], expected: str, semantic: Any +) -> None: + actual = api.remove_value_segments(source, segments) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "new_key", "expected", "semantic"), + [ + # Preserve dotted prefix spelling and intervening whitespace, replace only last segment. + ('a . b : 1\n', ["a", "b"], "c", 'a . c : 1\n', {"a": {"c": 1}}), + ('"a.b" . old : 1\n', ["a.b", "old"], "new key", '"a.b" . new key : 1\n', {"a.b": {"new key": 1}}), + # Rename in place: all surrounding member order and bytes stay stable. + ("one: 1\ntwo: 2\nthree: 3\n", ["two"], "second", "one: 1\nsecond: 2\nthree: 3\n", {"one": 1, "second": 2, "three": 3}), + ], +) +def test_rename_source_dotted_segment_geometry( + api: Any, source: str, segments: list[str], new_key: str, expected: str, semantic: Any +) -> None: + actual = api.rename_key_segments(source, segments, new_key) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "value", "expected", "semantic"), + [ + # Empty explicit root uses the four-space default indentation. + ("{\n}\n", ["x"], 1, "{\n x: 1\n}\n", {"x": 1}), + # Root insertion retains NBSP indentation before the explicit closer. + ("{\n\u00a0}\n", ["x"], 1, "{\n x: 1\n\u00a0}\n", {"x": 1}), + # Mixed EOL nested parent keeps CRLF geometry and the LF-only tail. + ("root: {\r\n child: {\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", ["root", "child", "x"], 1, "root: {\r\n child: {\r\n x: 1\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", {"root": {"child": {"x": 1}}, "keep": 1, "tail": 2}), + ], +) +def test_insert_source_geometry_materialization( + api: Any, source: str, segments: list[str], value: Any, expected: str, semantic: Any +) -> None: + actual = api.insert_value_segments(source, segments, value) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +def test_insert_nested_block_array_object_materialization(api: Any) -> None: + source = "root: {\n list: [1]\n}\n" + expected = "root: {\n list: [1]\n child: {\n value: 2\n }\n}\n" + actual = api.insert_value_segments(source, ["root", "child"], {"value": 2}) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"root": {"list": [1], "child": {"value": 2}}} + + +@pytest.mark.parametrize("api", APIS) +def test_insert_nested_object_inside_array_object(api: Any) -> None: + source = "items: [{name: one}]\n" + expected = "items: [{name: one, value: 2}]\n" + actual = api.insert_value_segments(source, ["items", "0", "value"], 2) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"items": [{"name": "one", "value": 2}]} + + +@pytest.mark.parametrize("api", APIS) +def test_mutation_collision_order_and_noop(api: Any) -> None: + source = "first: 1\nsecond: 2\n" + assert api.rename_key_segments(source, ["first"], "first").encode("utf-8") == source.encode("utf-8") + with pytest.raises(api.KtavEditError) as caught: + api.rename_key_segments(source, ["first"], "second") + assert caught.value.error == "EditAlreadyExists" + with pytest.raises(api.KtavEditError) as caught: + api.insert_value_segments(source, ["second"], 3) + assert caught.value.error == "EditAlreadyExists" + actual = api.insert_value_segments(source, ["third"], 3) + assert actual.encode("utf-8") == b"first: 1\nsecond: 2\nthird: 3\n" + assert api.loads(actual) == {"first": 1, "second": 2, "third": 3} diff --git a/tests/edit/test_pure_mutations_regressions.py b/tests/edit/test_pure_mutations_regressions.py new file mode 100644 index 0000000..af0187c --- /dev/null +++ b/tests/edit/test_pure_mutations_regressions.py @@ -0,0 +1,113 @@ +"""Exact-byte and independent semantic remove/rename regression oracles.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "expected", "semantic"), + [ + ("## keep\na: [1, 2, 3]\nb: 4\n", ["a", "1"], "## keep\na: [1, 3]\nb: 4\n", {"a": [1, 3], "b": 4}), + ("root: {child: {leaf: 1}}\nkeep: 2\n", ["root", "child", "leaf"], "root: {child: {}}\nkeep: 2\n", {"root": {"child": {}}, "keep": 2}), + ('"a.b": {x: 1, y: 2}\n', ["a.b", "x"], '"a.b": {y: 2}\n', {"a.b": {"y": 2}}), + ("a: 1\nb: 2\n", ["a"], "b: 2\n", {"b": 2}), + ], +) +def test_remove_exact_bytes_and_semantics(api: Any, source: str, segments: list[str], expected: str, semantic: Any) -> None: + actual = api.remove_value_segments(source, segments) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "segments", "new_key", "expected", "semantic"), + [ + ('"a.b": {x: 1, y: 2}\n', ["a.b", "x"], "z", '"a.b": {z: 1, y: 2}\n', {"a.b": {"z": 1, "y": 2}}), + ('"a.b": 1\n', ["a.b"], "c.d", '"c.d": 1\n', {"c.d": 1}), + ("one: 1\ntwo: 2\nthree: 3\n", ["two"], "second", "one: 1\nsecond: 2\nthree: 3\n", {"one": 1, "second": 2, "three": 3}), + ("a:: 042\n", ["a"], "c.d", "\"c.d\":: 042\n", {"c.d": "042"}), + ], +) +def test_rename_exact_bytes_and_semantics(api: Any, source: str, segments: list[str], new_key: str, expected: str, semantic: Any) -> None: + actual = api.rename_key_segments(source, segments, new_key) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +@pytest.mark.parametrize( + ("source", "expected", "semantic"), + [ + ("## leading\nroot: 1 \nkeep: 2\n", "## leading\nkeep: 2\n", {"keep": 2}), + ("\ufeff## leading\nroot: {child: 1} \nkeep: 2\n", "\ufeff## leading\nkeep: 2\n", {"keep": 2}), + ], +) +def test_remove_inline_root_after_leading_comments_exact_bytes_and_semantics( + api: Any, source: str, expected: str, semantic: Any +) -> None: + actual = api.remove_value_segments(source, ["root"]) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == semantic + + +@pytest.mark.parametrize("api", APIS) +def test_remove_inline_root_after_leading_comments_pure_exact_bytes_and_semantics(api: Any) -> None: + source = '\ufeff## comment\n {a: 1, b: 2} \n' + expected = '\ufeff## comment\n {b: 2} \n' + actual = api.remove_value_segments(source, ["a"]) + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"b": 2} + + +@pytest.mark.parametrize("api", APIS) +def test_remove_and_rename_dotted_paths_and_eol(api: Any) -> None: + source = '"a.b": {child: {leaf: 1}, keep: 2}\r\n' + actual = api.rename_key_segments(source, ["a.b", "child", "leaf"], "changed") + expected = '"a.b": {child: {changed: 1}, keep: 2}\r\n' + assert actual.encode("utf-8") == expected.encode("utf-8") + assert api.loads(actual) == {"a.b": {"child": {"changed": 1}, "keep": 2}} + with pytest.raises(api.KtavEditError): + api.remove_value_segments("a: 1\n", ["missing", "child"]) + + + + +@pytest.mark.parametrize("api", APIS) +def test_comment_compound_mutations(api: Any) -> None: + source = "a: {\n ## note\n x: 1\n}\n" + with pytest.raises(api.KtavEditError) as caught: + api.remove_value_segments(source, ["a"]) + assert caught.value.error == "EditUnsupported" + + with pytest.raises(api.KtavEditError) as caught: + api.rename_key_segments("a: 1\nb: 2\n", ["a"], "b") + assert caught.value.error == "EditAlreadyExists" + + +@pytest.mark.parametrize("api", APIS) +def test_remove_materializes_empty_dotted_parent(api: Any) -> None: + actual = api.remove_value_segments("a.b: 1\n", ["a", "b"]) + assert actual.encode("utf-8") == "a: {}\n".encode("utf-8") + assert api.loads(actual) == {"a": {}} + + +@pytest.mark.parametrize("api", APIS) +def test_source_parse_error_precedes_bad_mutation_paths(api: Any) -> None: + for operation, args in ( + ("remove_value", ("a: {", "a..b")), + ("rename_key", ("a: {", "a..b", "z")), + ("remove_value_segments", ("a: {", ["a", ""])), + ("rename_key_segments", ("a: {", ["a", ""], "z")), + ): + with pytest.raises(api.KtavDecodeError) as caught: + getattr(api, operation)(*args) + assert caught.value.error == "UnclosedCompound" diff --git a/tests/edit/test_source_mutations.py b/tests/edit/test_source_mutations.py new file mode 100644 index 0000000..3ed546c --- /dev/null +++ b/tests/edit/test_source_mutations.py @@ -0,0 +1,92 @@ +"""Source-mutation regression oracles from Rust source-mutate09. + +The APIs are expected to be exposed by each backend as insert_value[ _segments], +remove_value[ _segments], and rename_key[ _segments]. Literal segment APIs take +already-decoded Python string sequences (not JSON transport bytes). +""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.edit._backends import edit_apis + +APIS = edit_apis() +ERROR_FIELDS = ( + "error", "reason", "line", "line_text", "span", "path", "body", + "canonical", "spec_section", "message", +) + + +@pytest.mark.parametrize("api", APIS) +def test_insert_text_and_literal_segment_paths(api: Any) -> None: + source = '"a.b": {old: 1}\na: {b: {x: 2}}\n' + expected = '"a.b": {old: 1, new: 3}\na: {b: {x: 2}}\n' + assert api.insert_value(source, r"a\.b.new", 3) == expected + assert api.insert_value_segments(source, ["a.b", "new"], 3) == expected + assert api.insert_value("a: 1\n", r"x\.y", 2) == 'a: 1\n"x.y": 2\n' + assert api.insert_value_segments("a: 1\n", ["x.y"], 2) == 'a: 1\n"x.y": 2\n' + assert api.insert_value("a: {x: 1}\n", "a.y", "two") == "a: {x: 1, y: two}\n" + + +@pytest.mark.parametrize("api", APIS) +def test_remove_text_and_literal_segments(api: Any) -> None: + source = "## keep\na: [1, 2, 3]\nb: 4\n" + expected = "## keep\na: [1, 3]\nb: 4\n" + assert api.remove_value(source, "a.1") == expected + assert api.remove_value_segments(source, ["a", "1"]) == expected + assert api.remove_value("a: 1\nb: 2\n", "a") == "b: 2\n" + with pytest.raises(api.KtavEditError) as exc_info: + api.remove_value_segments("a: 1\n", []) + assert exc_info.value.error == "EditNotFound" + + +@pytest.mark.parametrize("api", APIS) +def test_rename_text_and_literal_segments(api: Any) -> None: + source = '"a.b": {x: 1, y: 2}\na: {b: {x: 3}}\n' + expected = '"a.b": {z: 1, y: 2}\na: {b: {x: 3}}\n' + assert api.rename_key(source, r"a\.b.x", "z") == expected + assert api.rename_key_segments(source, ["a.b", "x"], "z") == expected + assert api.rename_key("k:: 042\r\n", "k", "k") == "k:: 042\r\n" + assert api.rename_key_segments("a: {x: 1, y: 2}\n", ["a", "x"], "w") == ( + "a: {w: 1, y: 2}\n" + ) + assert api.rename_key("a: 1\n", "a", "ключ") == "ключ: 1\n" + + +@pytest.mark.parametrize("api", APIS) +def test_mutation_errors_have_typed_edit_contract(api: Any) -> None: + cases = ( + ("insert_value", ("a: 1\n", "a", 2), "EditAlreadyExists"), + ("insert_value", ("a: 1\n", "b.c", 2), "EditNotFound"), + ("remove_value", ("a: 1\n", "missing"), "EditNotFound"), + ("rename_key", ("a: 1\nb: 2\n", "a", "b"), "EditAlreadyExists"), + ) + for name, args, code in cases: + with pytest.raises(api.KtavEditError) as exc_info: + getattr(api, name)(*args) + error = exc_info.value + assert all(hasattr(error, field) for field in ERROR_FIELDS) + assert error.error == code + if code == "EditAlreadyExists": + assert isinstance(error, api.KtavAlreadyExistsError) + assert isinstance(error.message, str) and error.message + assert error.reason is None + assert error.line is None + assert error.line_text is None + assert error.span is None + assert error.path is None + assert error.canonical is None + assert error.spec_section is None + + +@pytest.mark.parametrize("api", APIS) +def test_invalid_text_path_reports_invalid_path(api: Any) -> None: + with pytest.raises(api.KtavEditError) as exc_info: + api.insert_value("a: 1\n", "a..b", 2) + error = exc_info.value + assert error.error == "EditInvalidPath" + assert error.body == "2" + assert error.line is None and error.span is None From 10e21c08bef367678d68c3fd9a23b9a504c5952f Mon Sep 17 00:00:00 2001 From: Marat K Date: Tue, 27 Oct 2026 08:32:11 +0100 Subject: [PATCH 2/5] style(edit): remove the trailing blank line Co-Authored-By: Claude Opus 5.5 --- python/ktav/_pure/_edit_remove.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/ktav/_pure/_edit_remove.py b/python/ktav/_pure/_edit_remove.py index 7abb30e..61b3cd5 100644 --- a/python/ktav/_pure/_edit_remove.py +++ b/python/ktav/_pure/_edit_remove.py @@ -217,4 +217,3 @@ def remove_value_segments(source: str, segments: list[str]) -> str: if _same_parse(candidate, expected): return candidate raise _unsupported("semantic mismatch after splice; no lossless patch produced") - From cb1290865b65b4e1aa6da532bcf7cbb444949fd3 Mon Sep 17 00:00:00 2001 From: Marat K Date: Tue, 27 Oct 2026 09:48:31 +0100 Subject: [PATCH 3/5] style(edit): satisfy mutation lint rules without behavior changes Sort imports and public exports, wrap long lines, remove unused bindings and split composite assertions. Preserve every distinct regression oracle and remove only an exact duplicate parameter case. Co-Authored-By: Claude Opus 5.5 --- python/ktav/__init__.py | 51 ++++++-- python/ktav/_pure/__init__.py | 16 ++- python/ktav/_pure/_edit_layout.py | 8 +- python/ktav/_pure/_edit_remove.py | 50 +++++--- python/ktav/_pure/_edit_rename.py | 24 ++-- python/ktav/_pure/_edit_splice.py | 110 ++++++++++++++---- python/ktav/pure.py | 20 ++-- .../test_insert_parameterized_regressions.py | 6 +- .../edit/test_insert_remaining_regressions.py | 6 +- .../edit/test_insert_semantic_regressions.py | 22 +++- ...st_mutation_source_geometry_regressions.py | 58 +++++++-- tests/edit/test_pure_mutations_regressions.py | 54 ++++++--- tests/edit/test_source_mutations.py | 22 ++-- 13 files changed, 331 insertions(+), 116 deletions(-) diff --git a/python/ktav/__init__.py b/python/ktav/__init__.py index 47a7f9f..93994c3 100644 --- a/python/ktav/__init__.py +++ b/python/ktav/__init__.py @@ -50,12 +50,6 @@ ) from ktav._core import ( edit_value_segments as _edit_value_segments, - insert_value as _insert_value, - insert_value_segments as _insert_value_segments, - remove_value as _remove_value, - remove_value_segments as _remove_value_segments, - rename_key as _rename_key, - rename_key_segments as _rename_key_segments, ) from ktav._core import ( emit_canonical as _emit_canonical, @@ -63,12 +57,30 @@ from ktav._core import ( format as _format, ) + from ktav._core import ( + insert_value as _insert_value, + ) + from ktav._core import ( + insert_value_segments as _insert_value_segments, + ) from ktav._core import ( loads as _loads, ) from ktav._core import ( loads_strict as _loads_strict, ) + from ktav._core import ( + remove_value as _remove_value, + ) + from ktav._core import ( + remove_value_segments as _remove_value_segments, + ) + from ktav._core import ( + rename_key as _rename_key, + ) + from ktav._core import ( + rename_key_segments as _rename_key_segments, + ) _CORE_NAMES = ( "KtavAlreadyExistsError", @@ -130,7 +142,16 @@ def _no_native(name: str) -> Any: def stub(*args: Any, **kwargs: Any) -> Any: - if name in ("edit_value", "edit_value_segments", "insert_value", "insert_value_segments", "remove_value", "remove_value_segments", "rename_key", "rename_key_segments"): + if name in ( + "edit_value", + "edit_value_segments", + "insert_value", + "insert_value_segments", + "remove_value", + "remove_value_segments", + "rename_key", + "rename_key_segments", + ): raise ImportError( f"ktav.{name} requires a ktav._core built against the" " source-edit 0.9 core; the installed extension does not" @@ -240,17 +261,17 @@ def __getattr__(name: str) -> Any: "dumps_force_strings", "edit_value", "edit_value_segments", + "emit_canonical", + "format", "insert_value", "insert_value_segments", + "load", + "loads", + "loads_strict", "remove_value", "remove_value_segments", "rename_key", "rename_key_segments", - "emit_canonical", - "format", - "load", - "loads", - "loads_strict", ] @@ -452,6 +473,7 @@ def edit_value_segments(source: str | bytes | bytearray, segments: Any, replacem def insert_value(source: str | bytes | bytearray, path: str, replacement: Any) -> str: + """Insert a value at the textual ``path`` in ``source``.""" _load_core() text = _as_text(source) _loads(text) @@ -459,6 +481,7 @@ def insert_value(source: str | bytes | bytearray, path: str, replacement: Any) - def insert_value_segments(source: str | bytes | bytearray, segments: Any, replacement: Any) -> str: + """Insert a value using already-decoded literal path ``segments``.""" _load_core() text = _as_text(source) _loads(text) @@ -469,6 +492,7 @@ def insert_value_segments(source: str | bytes | bytearray, segments: Any, replac def remove_value(source: str | bytes | bytearray, path: str) -> str: + """Remove the value at the textual ``path`` in ``source``.""" _load_core() text = _as_text(source) _loads(text) @@ -476,6 +500,7 @@ def remove_value(source: str | bytes | bytearray, path: str) -> str: def remove_value_segments(source: str | bytes | bytearray, segments: Any) -> str: + """Remove a value using already-decoded literal path ``segments``.""" _load_core() text = _as_text(source) _loads(text) @@ -486,6 +511,7 @@ def remove_value_segments(source: str | bytes | bytearray, segments: Any) -> str def rename_key(source: str | bytes | bytearray, path: str, new_key: str) -> str: + """Rename the key at the textual ``path`` in ``source``.""" _load_core() text = _as_text(source) _loads(text) @@ -493,6 +519,7 @@ def rename_key(source: str | bytes | bytearray, path: str, new_key: str) -> str: def rename_key_segments(source: str | bytes | bytearray, segments: Any, new_key: str) -> str: + """Rename a key using already-decoded literal path ``segments``.""" _load_core() text = _as_text(source) _loads(text) diff --git a/python/ktav/_pure/__init__.py b/python/ktav/_pure/__init__.py index 65970ad..0b3ec05 100644 --- a/python/ktav/_pure/__init__.py +++ b/python/ktav/_pure/__init__.py @@ -75,16 +75,16 @@ def _as_str(s: str | bytes | bytearray | memoryview) -> str: "dumps_force_strings", "edit_value", "edit_value_segments", + "emit_canonical", + "format", "insert_value", "insert_value_segments", + "loads", + "loads_strict", "remove_value", "remove_value_segments", "rename_key", "rename_key_segments", - "emit_canonical", - "format", - "loads", - "loads_strict", ] __version__ = "0.8.1" @@ -186,7 +186,9 @@ def insert_value(source: str | bytes | bytearray | memoryview, path: str, value: return insert_value_segments(text, parse_path(path), value) -def insert_value_segments(source: str | bytes | bytearray | memoryview, segments: Any, value: Any) -> str: +def insert_value_segments( + source: str | bytes | bytearray | memoryview, segments: Any, value: Any +) -> str: from ._edit_splice import insert_value_segments as _insert text = _as_str(source) @@ -226,7 +228,9 @@ def rename_key(source: str | bytes | bytearray | memoryview, path: str, new_key: return rename_key_segments(text, parse_path(path), new_key) -def rename_key_segments(source: str | bytes | bytearray | memoryview, segments: Any, new_key: str) -> str: +def rename_key_segments( + source: str | bytes | bytearray | memoryview, segments: Any, new_key: str +) -> str: from ._edit_rename import rename_key_segments as _rename text = _as_str(source) diff --git a/python/ktav/_pure/_edit_layout.py b/python/ktav/_pure/_edit_layout.py index 033e959..0a4990f 100644 --- a/python/ktav/_pure/_edit_layout.py +++ b/python/ktav/_pure/_edit_layout.py @@ -78,7 +78,13 @@ def _slot_body(slot: tuple[Any, ...]) -> tuple[int, int]: return slot[2], slot[3] -def _inline_owner(source: str, root: Any, path: list[str], pos: int, trim_trailing: bool = False) -> tuple[int, int] | None: +def _inline_owner( + source: str, + root: Any, + path: list[str], + pos: int, + trim_trailing: bool = False, +) -> tuple[int, int] | None: node = root if root.inline: line_start, _line_end, raw_line = _physical_line_span(source, pos) diff --git a/python/ktav/_pure/_edit_remove.py b/python/ktav/_pure/_edit_remove.py index 61b3cd5..90980f3 100644 --- a/python/ktav/_pure/_edit_remove.py +++ b/python/ktav/_pure/_edit_remove.py @@ -1,20 +1,29 @@ """Pure-Python lossless positional removal edits.""" + from __future__ import annotations from typing import Any from ._edit_common import ( - _lookup_value, _not_found, _parse_index, _same, _unsupported, - _physical_line_span, _inline_segments, + _inline_segments, + _lookup_value, + _not_found, + _parse_index, + _physical_line_span, + _same, + _unsupported, ) from ._edit_tree import ( - _build_tree, _descend, _collect_multiline_scalar_ranges, _has_comment_line, + _build_tree, + _collect_multiline_scalar_ranges, + _descend, + _has_comment_line, ) -from .writer import push_key_segment from ._inline import ScanError +from ._text import WS_CHARS, WS_INLINE_CHARS from .errors import KtavDecodeError, KtavEditError from .parser import parse_document -from ._text import WS_CHARS, WS_INLINE_CHARS +from .writer import push_key_segment __all__ = ["remove_value_segments"] @@ -75,14 +84,16 @@ def _expected_without(original: Any, segments: list[str]) -> Any: return expected -def _inline_parent(source: str, root: Any, segments: list[str], explicit: bool, anchor: int) -> tuple[int, int]: +def _inline_parent( + source: str, root: Any, segments: list[str], explicit: bool, anchor: int +) -> tuple[int, int]: parent, _inline, _first = _descend(root, segments[:-1], explicit) if parent.slot is not None: slot = parent.slot if slot[0] != "block": return slot[2], slot[3] if not segments[:-1] and root.inline: - a, b, line = _physical_line_span(source, _line_start(source, anchor)) + a, _b, line = _physical_line_span(source, _line_start(source, anchor)) lead = len(line) - len(line.lstrip(WS_CHARS)) content = line[lead:] bom = 1 if content.startswith("\ufeff") else 0 @@ -95,7 +106,9 @@ def _inline_parent(source: str, root: Any, segments: list[str], explicit: bool, raise _unsupported("inline target without inline parent") -def _inline_range(source: str, root: Any, segments: list[str], explicit: bool, slot: tuple[Any, ...]) -> tuple[int, int]: +def _inline_range( + source: str, root: Any, segments: list[str], explicit: bool, slot: tuple[Any, ...] +) -> tuple[int, int]: anchor = slot[1] p0, p1 = _inline_parent(source, root, segments, explicit, anchor) body = source[p0:p1] @@ -141,7 +154,10 @@ def remove_value_segments(source: str, segments: list[str]) -> str: node, _inline, _root_first = _descend(root, list(segments), explicit) slot = node.slot if slot is None: - raise _unsupported("target is a noncontiguous value (dotted-key/reopen intermediate); no lossless single-range removal exists") + raise _unsupported( + "target is a noncontiguous value (dotted-key/reopen intermediate); " + "no lossless single-range removal exists" + ) if node.inline: start, end = _inline_range(source, root, list(segments), explicit, slot) else: @@ -149,7 +165,7 @@ def remove_value_segments(source: str, segments: list[str]) -> str: start = _line_start(source, anchor) if start == 0 and source.startswith("\ufeff"): start = 1 - end = _line_end(source, slot[3] if slot[0] != "block" else slot[3]) + end = _line_end(source, slot[3]) if end == len(source) and not source[start:end].endswith(("\n", "\r")): if start and source[start - 1] == "\n": start -= 1 @@ -161,7 +177,9 @@ def remove_value_segments(source: str, segments: list[str]) -> str: ranges: list[tuple[int, int]] = [] _collect_multiline_scalar_ranges(node, ranges) if _has_comment_line(source, start, end, ranges): - raise _unsupported("target contains comment lines; removal cannot preserve them losslessly") + raise _unsupported( + "target contains comment lines; removal cannot preserve them losslessly" + ) cut_candidate = source[:start] + source[end:] if _same_parse(cut_candidate, expected): @@ -173,7 +191,7 @@ def remove_value_segments(source: str, segments: list[str]) -> str: for first in range(len(ancestors)): all_empty = True for depth in range(first, len(ancestors)): - value = _lookup_value(expected, ancestors[:depth + 1]) + value = _lookup_value(expected, ancestors[: depth + 1]) if not isinstance(value, dict) or value: all_empty = False break @@ -182,18 +200,18 @@ def remove_value_segments(source: str, segments: list[str]) -> str: break if emptied_from is not None: m = emptied_from - synthetic, _inl, _first = _descend(root, ancestors[:m + 1], explicit) + synthetic, _inl, _first = _descend(root, ancestors[: m + 1], explicit) if synthetic.slot is None: container_depth = 0 for depth in range(len(ancestors)): - n, _, _ = _descend(root, ancestors[:depth + 1], explicit) + n, _, _ = _descend(root, ancestors[: depth + 1], explicit) if n.slot is not None: container_depth = depth + 1 if m >= container_depth: lead = 0 - if start < end and source[start:start + 2] == "\r\n": + if start < end and source[start : start + 2] == "\r\n": lead = 2 - elif start < end and source[start:start + 1] in ("\r", "\n"): + elif start < end and source[start : start + 1] in ("\r", "\n"): lead = 1 line_start = start + lead line_end = line_start diff --git a/python/ktav/_pure/_edit_rename.py b/python/ktav/_pure/_edit_rename.py index 7aac1d8..e5e2a81 100644 --- a/python/ktav/_pure/_edit_rename.py +++ b/python/ktav/_pure/_edit_rename.py @@ -4,11 +4,11 @@ from typing import Any -from ._edit_common import _lookup_value, _not_found, _parse_index, _unsupported, _same +from ._edit_common import _lookup_value, _not_found, _parse_index, _same, _unsupported from ._edit_tree import _build_tree, _descend from ._keys import split_key_path from ._text import is_inline_whitespace -from .errors import KtavDecodeError, KtavEditError, KtavAlreadyExistsError +from .errors import KtavAlreadyExistsError, KtavDecodeError, KtavEditError from .parser import parse_document from .writer import push_key_segment @@ -35,7 +35,9 @@ def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: key, parents = segments[-1], list(segments[:-1]) parent = _lookup_value(original, parents) if isinstance(parent, list): - raise _unsupported("rename of an Array element is not supported; only Object keys can be renamed") + raise _unsupported( + "rename of an Array element is not supported; only Object keys can be renamed" + ) if not isinstance(parent, dict) or key not in parent: raise _not_found() if key == new_key: @@ -52,9 +54,14 @@ def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: raise _not_found() slot = target.slot if slot is None: - raise _unsupported("target is a noncontiguous value (dotted-key/reopen intermediate); no lossless rename range exists") + raise _unsupported( + "target is a noncontiguous value (dotted-key/reopen intermediate); " + "no lossless rename range exists" + ) if slot[0] not in ("pair", "block") or (slot[0] == "block" and slot[1] is None): - raise _unsupported("rename of an Array element is not supported; only Object keys can be renamed") + raise _unsupported( + "rename of an Array element is not supported; only Object keys can be renamed" + ) colon = slot[1] # Find the final key segment within its pair, respecting inline # delimiters, quoted segments, escapes, dotted paths, and a leading BOM. @@ -88,10 +95,7 @@ def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: if source[i] == "\\": i += 2 continue - if source[i] == ".": - boundary = i + 1 - segment_start = True - elif source[i] in ",{[": + if source[i] == "." or source[i] in ",{[": boundary = i + 1 segment_start = True i += 1 @@ -108,7 +112,7 @@ def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: first_root_key = not parents and next(iter(parent_node.children), None) == key encoded: list[str] = [] push_key_segment(new_key, first_root_key, encoded) - result = source[:seg_start] + "".join(encoded) + source[seg_start + len(last):] + result = source[:seg_start] + "".join(encoded) + source[seg_start + len(last) :] expected = _clone(original) expect_parent = expected diff --git a/python/ktav/_pure/_edit_splice.py b/python/ktav/_pure/_edit_splice.py index 237f7c9..1fa40ac 100644 --- a/python/ktav/_pure/_edit_splice.py +++ b/python/ktav/_pure/_edit_splice.py @@ -29,17 +29,23 @@ _PlanCtx, ) from ._edit_layout import _layout_adjust -from ._text import WS_CHARS from ._edit_tree import ( _build_tree, _collect_multiline_scalar_ranges, _descend, _has_comment_line, ) -from .errors import KtavDecodeError, KtavEditError +from ._text import WS_CHARS +from .errors import KtavDecodeError from .parser import parse_document -__all__ = ["apply_edit", "edit_value", "edit_value_segments", "insert_value", "insert_value_segments"] +__all__ = [ + "apply_edit", + "edit_value", + "edit_value_segments", + "insert_value", + "insert_value_segments", +] def _root_closer_line_start(text: str) -> int | None: @@ -66,9 +72,12 @@ def _sibling_indent(text: str, node: Any) -> str | None: pos = slot[1] if slot[0] in ("pair", "item") else slot[2] return _line_indent(text, _line_start(text, pos)) + def _block_encodable(value: Any) -> bool: if isinstance(value, str): - return "\r" not in value and not any(line.strip(" \t") == "))" for line in value.split("\n")) + return "\r" not in value and not any( + line.strip(" \t") == "))" for line in value.split("\n") + ) if isinstance(value, dict): return all(_block_encodable(child) for child in value.values()) if isinstance(value, list): @@ -78,7 +87,7 @@ def _block_encodable(value: Any) -> bool: def _insert_pair(key: str, value: Any, indent: str, eol: str, inline: bool) -> str: """Render one newly inserted object member in block or inline form.""" - from ._edit_encode import _PlanCtx, _plan_value, _inline_body + from ._edit_encode import _inline_body, _plan_value, _PlanCtx from .writer import push_key_segment parts: list[str] = [] @@ -248,8 +257,6 @@ def edit_value_segments(source: str, segments: list[str], replacement: Any) -> s return _edit_segments_checked(source, list(segments), replacement) - - def _edit_segments_checked(source: str, segments: list[str], replacement: Any) -> str: from .writer import to_value @@ -266,11 +273,10 @@ def insert_value(source: str, path: str, value: Any) -> str: def insert_value_segments(source: str, segments: list[str], value: Any) -> str: """Losslessly append a new member to an existing Object.""" - from .writer import to_value - from ._edit_common import _line_start, _line_indent, _detect_eol, _parse_index + from ._edit_common import _detect_eol, _line_indent, _line_start, _parse_index from ._edit_layout import _requires_block from .errors import KtavAlreadyExistsError, KtavEditError - + from .writer import to_value original = parse_document(source) if not segments: @@ -283,7 +289,9 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: key, parents = decoded[-1], decoded[:-1] parent = _lookup_value(original, parents) if isinstance(parent, list): - raise _unsupported("insertion into an Array is not supported; only Object members can be created") + raise _unsupported( + "insertion into an Array is not supported; only Object members can be created" + ) if not isinstance(parent, dict): raise _not_found() if key in parent: @@ -291,7 +299,13 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: root, root_explicit = _build_tree(source) node = root for seg in parents: - node = node.child(seg) if node.kind == "object" else node.item(_parse_index(seg) if _parse_index(seg) is not None else -1) if node.kind == "array" else None + if node.kind == "object": + node = node.child(seg) + elif node.kind == "array": + index = _parse_index(seg) + node = node.item(index if index is not None else -1) + else: + node = None if node is None: raise _not_found() eol = _detect_eol(source, len(source)) @@ -303,6 +317,7 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: inline_body = (b0, b1) elif not parents and root.inline: from ._edit_common import _physical_line_span + line_start = 0 while line_start < len(source): _ls, _le, raw_line = _physical_line_span(source, line_start) @@ -312,7 +327,12 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: line = line.strip(WS_CHARS) if not line or line.startswith("#"): line_start = _le + (1 if _le < len(source) else 0) - if _le < len(source) and source[_le] == "\r" and _le + 1 < len(source) and source[_le + 1] == "\n": + if ( + _le < len(source) + and source[_le] == "\r" + and _le + 1 < len(source) + and source[_le + 1] == "\n" + ): line_start += 1 continue leading = len(raw_line) - len(raw_line.lstrip(WS_CHARS)) @@ -320,7 +340,7 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: if _ls == 0: bom_end = start + len(raw_line[leading:]) - len(raw_line[leading:].lstrip("\ufeff")) start = bom_end - start += len(raw_line[start - _ls:]) - len(raw_line[start - _ls:].lstrip(WS_CHARS)) + start += len(raw_line[start - _ls :]) - len(raw_line[start - _ls :].lstrip(WS_CHARS)) if line.startswith("{") and line.endswith("}"): inline_body = (start, start + len(line)) break @@ -329,7 +349,9 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: eol = _detect_eol(source, b) inner = source[a + 1 : b - 1] indent = _line_indent(source, a) - wants_block = _requires_block(replacement, 0) or (isinstance(replacement, str) and ("\n" in replacement or "\r" in replacement)) + wants_block = _requires_block(replacement, 0) or ( + isinstance(replacement, str) and ("\n" in replacement or "\r" in replacement) + ) force = wants_block and _block_encodable(replacement) pair = _insert_pair(key, replacement, indent, eol, True) trimmed = inner.rstrip(WS_CHARS) @@ -349,7 +371,9 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: else: at, to, inserted = b - 1, b - 1, ", " + pair result = source[:at] + inserted + source[to:] - return _verify_insert_result(source, result, decoded, replacement, original, root, at, to, inserted, force) + return _verify_insert_result( + source, result, decoded, replacement, original, root, at, to, inserted, force + ) if not parents and root_explicit: closer = _root_closer_line_start(source) if closer is None: @@ -358,15 +382,32 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: if indent is None: indent = " " at = closer - inserted = indent + _insert_pair(key, replacement, indent, _detect_eol(source, at), False) + _detect_eol(source, at) + inserted = ( + indent + + _insert_pair(key, replacement, indent, _detect_eol(source, at), False) + + _detect_eol(source, at) + ) result = source[:at] + inserted + source[at:] - return _verify_insert_result(source, result, decoded, replacement, original, root, at, at, inserted, False) + return _verify_insert_result( + source, result, decoded, replacement, original, root, at, at, inserted, False + ) if not parents: at = len(source) pair = _insert_pair(key, replacement, "", eol, False) lead = "" if not source or source.endswith(("\n", "\r")) else eol tail = eol if source and source.endswith(("\n", "\r")) else "" - return _verify_insert_result(source, source + lead + pair + tail, decoded, replacement, original, root, at, at, lead + pair + tail, False) + return _verify_insert_result( + source, + source + lead + pair + tail, + decoded, + replacement, + original, + root, + at, + at, + lead + pair + tail, + False, + ) if slot is None or slot[0] != "block": raise _unsupported("parent is a noncontiguous value; no lossless insertion point exists") end = slot[3] @@ -378,7 +419,9 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: pair_key = _insert_pair(key, replacement, indent, eol, False) inserted = indent + pair_key + eol result = source[:at] + inserted + source[at:] - return _verify_insert_result(source, result, decoded, replacement, original, root, at, at, inserted, False) + return _verify_insert_result( + source, result, decoded, replacement, original, root, at, at, inserted, False + ) def _clone_value(value: Any) -> Any: @@ -389,14 +432,33 @@ def _clone_value(value: Any) -> Any: return value -def _verify_insert_result(source: str, result: str, segments: list[str], replacement: Any, original: Any, root: Any, frm: int, to: int, inserted: str, force: bool = False) -> str: +def _verify_insert_result( + source: str, + result: str, + segments: list[str], + replacement: Any, + original: Any, + root: Any, + frm: int, + to: int, + inserted: str, + force: bool = False, +) -> str: from ._edit_common import _physical_line from ._edit_layout import _layout_adjust + old_line = _physical_line(source, frm) new_line = _physical_line(result, frm) - if force or len(new_line) > max(len(old_line), 100): - if _block_encodable(replacement): - result = _layout_adjust(source, result, root, segments[:-1], replacement, (frm, to, len(inserted)), trim_owner_trailing=True) + if (force or len(new_line) > max(len(old_line), 100)) and _block_encodable(replacement): + result = _layout_adjust( + source, + result, + root, + segments[:-1], + replacement, + (frm, to, len(inserted)), + trim_owner_trailing=True, + ) try: actual_doc = parse_document(result) except KtavDecodeError as exc: diff --git a/python/ktav/pure.py b/python/ktav/pure.py index a3569be..e06f423 100644 --- a/python/ktav/pure.py +++ b/python/ktav/pure.py @@ -33,17 +33,17 @@ dumps_force_strings, edit_value, edit_value_segments, + emit_canonical, + format, insert_value, insert_value_segments, + load, + loads, + loads_strict, remove_value, remove_value_segments, rename_key, rename_key_segments, - emit_canonical, - format, - load, - loads, - loads_strict, ) __all__ = [ @@ -63,15 +63,15 @@ "dumps_force_strings", "edit_value", "edit_value_segments", + "emit_canonical", + "format", "insert_value", "insert_value_segments", + "load", + "loads", + "loads_strict", "remove_value", "remove_value_segments", "rename_key", "rename_key_segments", - "emit_canonical", - "format", - "load", - "loads", - "loads_strict", ] diff --git a/tests/edit/test_insert_parameterized_regressions.py b/tests/edit/test_insert_parameterized_regressions.py index ed4a65b..2de6946 100644 --- a/tests/edit/test_insert_parameterized_regressions.py +++ b/tests/edit/test_insert_parameterized_regressions.py @@ -38,7 +38,11 @@ def test_insert_exact_bytes_and_semantics( @pytest.mark.parametrize("api", APIS) @pytest.mark.parametrize( ("source", "path", "code"), - [("a: 1\n", "a", "EditAlreadyExists"), ("a: 1\n", "b.c", "EditNotFound"), ("a: 1\n", "a..b", "EditInvalidPath")], + [ + ("a: 1\n", "a", "EditAlreadyExists"), + ("a: 1\n", "b.c", "EditNotFound"), + ("a: 1\n", "a..b", "EditInvalidPath"), + ], ) def test_insert_error_contract(api: Any, source: str, path: str, code: str) -> None: with pytest.raises(api.KtavEditError) as caught: diff --git a/tests/edit/test_insert_remaining_regressions.py b/tests/edit/test_insert_remaining_regressions.py index 9aee783..c316b1b 100644 --- a/tests/edit/test_insert_remaining_regressions.py +++ b/tests/edit/test_insert_remaining_regressions.py @@ -30,7 +30,7 @@ def test_insert_parse_error_offsets_count_utf8_bytes(api: Any) -> None: error = caught.value assert error.error == "UnclosedCompound" assert error.span == { - "start": len("## café\na: ".encode("utf-8")), + "start": len("## café\na: ".encode()), "end": len(broken.encode("utf-8")), } @@ -108,7 +108,9 @@ def test_insert_multiline_value_uses_block_form(api: Any) -> None: source = "items: { old: 1, keep: [1, 2] }\n" value = "first\nsecond" actual = api.insert_value_segments(source, ["items", "new"], value) - assert actual == "items: {\n old: 1\n keep: [1, 2]\n new: ((\nfirst\nsecond\n ))\n}\n" + assert actual == ( + "items: {\n old: 1\n keep: [1, 2]\n new: ((\nfirst\nsecond\n ))\n}\n" + ) assert api.loads(actual) == {"items": {"old": 1, "keep": [1, 2], "new": value}} diff --git a/tests/edit/test_insert_semantic_regressions.py b/tests/edit/test_insert_semantic_regressions.py index 3159e96..5d2df68 100644 --- a/tests/edit/test_insert_semantic_regressions.py +++ b/tests/edit/test_insert_semantic_regressions.py @@ -29,7 +29,13 @@ ("{a: 1,}\n", ["b"], 2, "{a: 1, b: 2,}\n", {"a": 1, "b": 2}), ("a: 1\n", ["", "b"], 2, "EditNotFound", None), ("## café\na: 1\n", ["b"], 2, "## café\na: 1\nb: 2\n", {"a": 1, "b": 2}), - ("a: 1\n", ["big"], 9223372036854775807, "a: 1\nbig: 9223372036854775807\n", {"a": 1, "big": 9223372036854775807}), + ( + "a: 1\n", + ["big"], + 9223372036854775807, + "a: 1\nbig: 9223372036854775807\n", + {"a": 1, "big": 9223372036854775807}, + ), ("", ["a", "b"], 1, "EditNotFound", None), ("a: {}\n", ["a", "b"], 1, "a: {b: 1}\n", {"a": {"b": 1}}), ("", ["a"], 1, "a: 1", {"a": 1}), @@ -37,10 +43,16 @@ ("a: 1\n", ["missing", "child"], 2, "EditNotFound", None), ("arr: [1, 2]\n", ["arr", "2"], 3, "EditUnsupported", None), ("a: 1\n", [], 2, "EditNotFound", None), - ("", ["a", "b"], 1, "EditNotFound", None), ], ) -def test_insert_exact_and_semantic_oracles(api: Any, source: str, segments: list[str], value: Any, expected: Any, semantic: Any) -> None: +def test_insert_exact_and_semantic_oracles( + api: Any, + source: str, + segments: list[str], + value: Any, + expected: Any, + semantic: Any, +) -> None: if semantic is None: with pytest.raises(api.KtavEditError) as caught: api.insert_value_segments(source, segments, value) @@ -55,7 +67,7 @@ def test_insert_exact_and_semantic_oracles(api: Any, source: str, segments: list def test_insert_root_comments_preserved_pure(api: Any) -> None: source = "## { lead }\n{a: 1}\n## { tail }\n" actual = api.insert_value_segments(source, ["b"], 2) - assert actual.encode("utf-8") == "## { lead }\n{a: 1, b: 2}\n## { tail }\n".encode("utf-8") + assert actual.encode("utf-8") == b"## { lead }\n{a: 1, b: 2}\n## { tail }\n" assert api.loads(actual) == {"a": 1, "b": 2} @@ -63,7 +75,7 @@ def test_insert_root_comments_preserved_pure(api: Any) -> None: def test_insert_root_leading_comment_pure(api: Any) -> None: source = "## only comment\n" actual = api.insert_value_segments(source, ["b"], 2) - assert actual.encode("utf-8") == "## only comment\nb: 2\n".encode("utf-8") + assert actual.encode("utf-8") == b"## only comment\nb: 2\n" assert api.loads(actual) == {"b": 2} diff --git a/tests/edit/test_mutation_source_geometry_regressions.py b/tests/edit/test_mutation_source_geometry_regressions.py index 864f1ad..2a1c0d3 100644 --- a/tests/edit/test_mutation_source_geometry_regressions.py +++ b/tests/edit/test_mutation_source_geometry_regressions.py @@ -18,15 +18,35 @@ # KTAV whitespace before explicit closer; dotted key and closer bytes survive. ("a: {\n\u00a0x: 1\n\u00a0}\n", ["a", "x"], "a: {\n\u00a0}\n", {"a": {}}), # Mixed EOLs: nested target is removed without normalizing the LF tail. - ("a: {\r\n b: {\r\n x: 1\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", ["a", "b", "x"], "a: {\r\n b: {\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", {"a": {"b": {}}, "keep": 2, "tail": 3}), + ( + "a: {\r\n b: {\r\n x: 1\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", + ["a", "b", "x"], + "a: {\r\n b: {\r\n }\r\n}\r\nkeep: 2\ntail: 3\n", + {"a": {"b": {}}, "keep": 2, "tail": 3}, + ), # Deep dotted deletion materializes only the emptied ancestor chain. ("a.b.c: 1\n", ["a", "b", "c"], "a.b: {}\n", {"a": {"b": {}}}), - ("keep: 0\r\na.b.c: 1", ["a", "b", "c"], "keep: 0\r\na.b: {}", {"keep": 0, "a": {"b": {}}}), + ( + "keep: 0\r\na.b.c: 1", + ["a", "b", "c"], + "keep: 0\r\na.b: {}", + {"keep": 0, "a": {"b": {}}}, + ), # Dotted deletion in an NBSP-indented nested block materializes ancestors. - ("outer: {\n\u00a0a.b.c: 1\n}\n", ["outer", "a", "b", "c"], "outer: {\n\u00a0a.b: {}\n}\n", {"outer": {"a": {"b": {}}}}), + ( + "outer: {\n\u00a0a.b.c: 1\n}\n", + ["outer", "a", "b", "c"], + "outer: {\n\u00a0a.b: {}\n}\n", + {"outer": {"a": {"b": {}}}}, + ), # Deep dotted deletion materializes through CRLF EOF without adding a final EOL. ("a.b.c: 1\r\n", ["a", "b", "c"], "a.b: {}\r\n", {"a": {"b": {}}}), - ("items: [\r\n {\r\n a.b: 1\r\n }\r\n]\r\n", ["items", "0", "a", "b"], "items: [\r\n {\r\n a: {}\r\n }\r\n]\r\n", {"items": [{"a": {}}]}), + ( + "items: [\r\n {\r\n a.b: 1\r\n }\r\n]\r\n", + ["items", "0", "a", "b"], + "items: [\r\n {\r\n a: {}\r\n }\r\n]\r\n", + {"items": [{"a": {}}]}, + ), # Removing final line without EOL keeps EOF unterminated. ("keep: 1\nlast: 2", ["last"], "keep: 1", {"keep": 1}), # Removing an earlier final-line target preserves the last line and its EOL state. @@ -46,10 +66,22 @@ def test_remove_source_geometry_exact_bytes_and_semantics( ("source", "segments", "new_key", "expected", "semantic"), [ # Preserve dotted prefix spelling and intervening whitespace, replace only last segment. - ('a . b : 1\n', ["a", "b"], "c", 'a . c : 1\n', {"a": {"c": 1}}), - ('"a.b" . old : 1\n', ["a.b", "old"], "new key", '"a.b" . new key : 1\n', {"a.b": {"new key": 1}}), + ("a . b : 1\n", ["a", "b"], "c", "a . c : 1\n", {"a": {"c": 1}}), + ( + '"a.b" . old : 1\n', + ["a.b", "old"], + "new key", + '"a.b" . new key : 1\n', + {"a.b": {"new key": 1}}, + ), # Rename in place: all surrounding member order and bytes stay stable. - ("one: 1\ntwo: 2\nthree: 3\n", ["two"], "second", "one: 1\nsecond: 2\nthree: 3\n", {"one": 1, "second": 2, "three": 3}), + ( + "one: 1\ntwo: 2\nthree: 3\n", + ["two"], + "second", + "one: 1\nsecond: 2\nthree: 3\n", + {"one": 1, "second": 2, "three": 3}, + ), ], ) def test_rename_source_dotted_segment_geometry( @@ -69,7 +101,13 @@ def test_rename_source_dotted_segment_geometry( # Root insertion retains NBSP indentation before the explicit closer. ("{\n\u00a0}\n", ["x"], 1, "{\n x: 1\n\u00a0}\n", {"x": 1}), # Mixed EOL nested parent keeps CRLF geometry and the LF-only tail. - ("root: {\r\n child: {\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", ["root", "child", "x"], 1, "root: {\r\n child: {\r\n x: 1\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", {"root": {"child": {"x": 1}}, "keep": 1, "tail": 2}), + ( + "root: {\r\n child: {\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", + ["root", "child", "x"], + 1, + "root: {\r\n child: {\r\n x: 1\r\n }\r\n}\r\nkeep: 1\ntail: 2\n", + {"root": {"child": {"x": 1}}, "keep": 1, "tail": 2}, + ), ], ) def test_insert_source_geometry_materialization( @@ -101,7 +139,9 @@ def test_insert_nested_object_inside_array_object(api: Any) -> None: @pytest.mark.parametrize("api", APIS) def test_mutation_collision_order_and_noop(api: Any) -> None: source = "first: 1\nsecond: 2\n" - assert api.rename_key_segments(source, ["first"], "first").encode("utf-8") == source.encode("utf-8") + assert api.rename_key_segments(source, ["first"], "first").encode("utf-8") == source.encode( + "utf-8" + ) with pytest.raises(api.KtavEditError) as caught: api.rename_key_segments(source, ["first"], "second") assert caught.value.error == "EditAlreadyExists" diff --git a/tests/edit/test_pure_mutations_regressions.py b/tests/edit/test_pure_mutations_regressions.py index af0187c..ea92bab 100644 --- a/tests/edit/test_pure_mutations_regressions.py +++ b/tests/edit/test_pure_mutations_regressions.py @@ -15,13 +15,25 @@ @pytest.mark.parametrize( ("source", "segments", "expected", "semantic"), [ - ("## keep\na: [1, 2, 3]\nb: 4\n", ["a", "1"], "## keep\na: [1, 3]\nb: 4\n", {"a": [1, 3], "b": 4}), - ("root: {child: {leaf: 1}}\nkeep: 2\n", ["root", "child", "leaf"], "root: {child: {}}\nkeep: 2\n", {"root": {"child": {}}, "keep": 2}), + ( + "## keep\na: [1, 2, 3]\nb: 4\n", + ["a", "1"], + "## keep\na: [1, 3]\nb: 4\n", + {"a": [1, 3], "b": 4}, + ), + ( + "root: {child: {leaf: 1}}\nkeep: 2\n", + ["root", "child", "leaf"], + "root: {child: {}}\nkeep: 2\n", + {"root": {"child": {}}, "keep": 2}, + ), ('"a.b": {x: 1, y: 2}\n', ["a.b", "x"], '"a.b": {y: 2}\n', {"a.b": {"y": 2}}), ("a: 1\nb: 2\n", ["a"], "b: 2\n", {"b": 2}), ], ) -def test_remove_exact_bytes_and_semantics(api: Any, source: str, segments: list[str], expected: str, semantic: Any) -> None: +def test_remove_exact_bytes_and_semantics( + api: Any, source: str, segments: list[str], expected: str, semantic: Any +) -> None: actual = api.remove_value_segments(source, segments) assert actual.encode("utf-8") == expected.encode("utf-8") assert api.loads(actual) == semantic @@ -31,13 +43,27 @@ def test_remove_exact_bytes_and_semantics(api: Any, source: str, segments: list[ @pytest.mark.parametrize( ("source", "segments", "new_key", "expected", "semantic"), [ - ('"a.b": {x: 1, y: 2}\n', ["a.b", "x"], "z", '"a.b": {z: 1, y: 2}\n', {"a.b": {"z": 1, "y": 2}}), + ( + '"a.b": {x: 1, y: 2}\n', + ["a.b", "x"], + "z", + '"a.b": {z: 1, y: 2}\n', + {"a.b": {"z": 1, "y": 2}}, + ), ('"a.b": 1\n', ["a.b"], "c.d", '"c.d": 1\n', {"c.d": 1}), - ("one: 1\ntwo: 2\nthree: 3\n", ["two"], "second", "one: 1\nsecond: 2\nthree: 3\n", {"one": 1, "second": 2, "three": 3}), - ("a:: 042\n", ["a"], "c.d", "\"c.d\":: 042\n", {"c.d": "042"}), + ( + "one: 1\ntwo: 2\nthree: 3\n", + ["two"], + "second", + "one: 1\nsecond: 2\nthree: 3\n", + {"one": 1, "second": 2, "three": 3}, + ), + ("a:: 042\n", ["a"], "c.d", '"c.d":: 042\n', {"c.d": "042"}), ], ) -def test_rename_exact_bytes_and_semantics(api: Any, source: str, segments: list[str], new_key: str, expected: str, semantic: Any) -> None: +def test_rename_exact_bytes_and_semantics( + api: Any, source: str, segments: list[str], new_key: str, expected: str, semantic: Any +) -> None: actual = api.rename_key_segments(source, segments, new_key) assert actual.encode("utf-8") == expected.encode("utf-8") assert api.loads(actual) == semantic @@ -48,7 +74,11 @@ def test_rename_exact_bytes_and_semantics(api: Any, source: str, segments: list[ ("source", "expected", "semantic"), [ ("## leading\nroot: 1 \nkeep: 2\n", "## leading\nkeep: 2\n", {"keep": 2}), - ("\ufeff## leading\nroot: {child: 1} \nkeep: 2\n", "\ufeff## leading\nkeep: 2\n", {"keep": 2}), + ( + "\ufeff## leading\nroot: {child: 1} \nkeep: 2\n", + "\ufeff## leading\nkeep: 2\n", + {"keep": 2}, + ), ], ) def test_remove_inline_root_after_leading_comments_exact_bytes_and_semantics( @@ -61,8 +91,8 @@ def test_remove_inline_root_after_leading_comments_exact_bytes_and_semantics( @pytest.mark.parametrize("api", APIS) def test_remove_inline_root_after_leading_comments_pure_exact_bytes_and_semantics(api: Any) -> None: - source = '\ufeff## comment\n {a: 1, b: 2} \n' - expected = '\ufeff## comment\n {b: 2} \n' + source = "\ufeff## comment\n {a: 1, b: 2} \n" + expected = "\ufeff## comment\n {b: 2} \n" actual = api.remove_value_segments(source, ["a"]) assert actual.encode("utf-8") == expected.encode("utf-8") assert api.loads(actual) == {"b": 2} @@ -79,8 +109,6 @@ def test_remove_and_rename_dotted_paths_and_eol(api: Any) -> None: api.remove_value_segments("a: 1\n", ["missing", "child"]) - - @pytest.mark.parametrize("api", APIS) def test_comment_compound_mutations(api: Any) -> None: source = "a: {\n ## note\n x: 1\n}\n" @@ -96,7 +124,7 @@ def test_comment_compound_mutations(api: Any) -> None: @pytest.mark.parametrize("api", APIS) def test_remove_materializes_empty_dotted_parent(api: Any) -> None: actual = api.remove_value_segments("a.b: 1\n", ["a", "b"]) - assert actual.encode("utf-8") == "a: {}\n".encode("utf-8") + assert actual.encode("utf-8") == b"a: {}\n" assert api.loads(actual) == {"a": {}} diff --git a/tests/edit/test_source_mutations.py b/tests/edit/test_source_mutations.py index 3ed546c..0931f48 100644 --- a/tests/edit/test_source_mutations.py +++ b/tests/edit/test_source_mutations.py @@ -15,8 +15,16 @@ APIS = edit_apis() ERROR_FIELDS = ( - "error", "reason", "line", "line_text", "span", "path", "body", - "canonical", "spec_section", "message", + "error", + "reason", + "line", + "line_text", + "span", + "path", + "body", + "canonical", + "spec_section", + "message", ) @@ -50,9 +58,7 @@ def test_rename_text_and_literal_segments(api: Any) -> None: assert api.rename_key(source, r"a\.b.x", "z") == expected assert api.rename_key_segments(source, ["a.b", "x"], "z") == expected assert api.rename_key("k:: 042\r\n", "k", "k") == "k:: 042\r\n" - assert api.rename_key_segments("a: {x: 1, y: 2}\n", ["a", "x"], "w") == ( - "a: {w: 1, y: 2}\n" - ) + assert api.rename_key_segments("a: {x: 1, y: 2}\n", ["a", "x"], "w") == ("a: {w: 1, y: 2}\n") assert api.rename_key("a: 1\n", "a", "ключ") == "ключ: 1\n" @@ -72,7 +78,8 @@ def test_mutation_errors_have_typed_edit_contract(api: Any) -> None: assert error.error == code if code == "EditAlreadyExists": assert isinstance(error, api.KtavAlreadyExistsError) - assert isinstance(error.message, str) and error.message + assert isinstance(error.message, str) + assert error.message assert error.reason is None assert error.line is None assert error.line_text is None @@ -89,4 +96,5 @@ def test_invalid_text_path_reports_invalid_path(api: Any) -> None: error = exc_info.value assert error.error == "EditInvalidPath" assert error.body == "2" - assert error.line is None and error.span is None + assert error.line is None + assert error.span is None From 6f12fe8d722e0c9399bff017eb2f4d7260e0392e Mon Sep 17 00:00:00 2001 From: Marat K Date: Tue, 27 Oct 2026 12:09:41 +0100 Subject: [PATCH 4/5] fix(pure): narrow mutation node and indent types Co-Authored-By: Claude Opus 5.5 --- python/ktav/_pure/_edit_rename.py | 2 +- python/ktav/_pure/_edit_splice.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/python/ktav/_pure/_edit_rename.py b/python/ktav/_pure/_edit_rename.py index e5e2a81..e4dae10 100644 --- a/python/ktav/_pure/_edit_rename.py +++ b/python/ktav/_pure/_edit_rename.py @@ -47,7 +47,7 @@ def rename_key_segments(source: str, segments: list[str], new_key: str) -> str: root, root_explicit = _build_tree(source) parent_node, _inline, _root_first = _descend(root, parents, root_explicit) - if parent_node.kind != "object": + if parent_node.kind != "object" or not isinstance(parent_node.children, dict): raise _unsupported("internal: parent node is not an object") target = parent_node.child(key) if target is None: diff --git a/python/ktav/_pure/_edit_splice.py b/python/ktav/_pure/_edit_splice.py index 1fa40ac..dbe8a9c 100644 --- a/python/ktav/_pure/_edit_splice.py +++ b/python/ktav/_pure/_edit_splice.py @@ -30,6 +30,7 @@ ) from ._edit_layout import _layout_adjust from ._edit_tree import ( + _Node, _build_tree, _collect_multiline_scalar_ranges, _descend, @@ -299,15 +300,17 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: root, root_explicit = _build_tree(source) node = root for seg in parents: + child: _Node | None if node.kind == "object": - node = node.child(seg) + child = node.child(seg) elif node.kind == "array": index = _parse_index(seg) - node = node.item(index if index is not None else -1) + child = node.item(index if index is not None else -1) else: - node = None - if node is None: + child = None + if child is None: raise _not_found() + node = child eol = _detect_eol(source, len(source)) slot = node.slot inline_body: tuple[int, int] | None = None @@ -378,9 +381,11 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: closer = _root_closer_line_start(source) if closer is None: raise _unsupported("internal: explicit root without closer") - indent = _sibling_indent(source, root) - if indent is None: + sibling_indent = _sibling_indent(source, root) + if sibling_indent is None: indent = " " + else: + indent = sibling_indent at = closer inserted = ( indent @@ -413,9 +418,11 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: end = slot[3] eol = _detect_eol(source, end) at = _line_start(source, end) - indent = _sibling_indent(source, node) - if indent is None: + sibling_indent = _sibling_indent(source, node) + if sibling_indent is None: indent = _line_indent(source, slot[2]) + " " + else: + indent = sibling_indent pair_key = _insert_pair(key, replacement, indent, eol, False) inserted = indent + pair_key + eol result = source[:at] + inserted + source[at:] From 96b27990c8631040f0204a2b1d1ddb32d36cfc61 Mon Sep 17 00:00:00 2001 From: Marat K Date: Tue, 27 Oct 2026 12:19:08 +0100 Subject: [PATCH 5/5] style(pure): preserve lint-clean type narrowing Co-Authored-By: Claude Opus 5.5 --- python/ktav/_pure/_edit_splice.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/python/ktav/_pure/_edit_splice.py b/python/ktav/_pure/_edit_splice.py index dbe8a9c..1f9339c 100644 --- a/python/ktav/_pure/_edit_splice.py +++ b/python/ktav/_pure/_edit_splice.py @@ -30,11 +30,11 @@ ) from ._edit_layout import _layout_adjust from ._edit_tree import ( - _Node, _build_tree, _collect_multiline_scalar_ranges, _descend, _has_comment_line, + _Node, ) from ._text import WS_CHARS from .errors import KtavDecodeError @@ -382,10 +382,7 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: if closer is None: raise _unsupported("internal: explicit root without closer") sibling_indent = _sibling_indent(source, root) - if sibling_indent is None: - indent = " " - else: - indent = sibling_indent + indent = " " if sibling_indent is None else sibling_indent at = closer inserted = ( indent @@ -419,10 +416,7 @@ def insert_value_segments(source: str, segments: list[str], value: Any) -> str: eol = _detect_eol(source, end) at = _line_start(source, end) sibling_indent = _sibling_indent(source, node) - if sibling_indent is None: - indent = _line_indent(source, slot[2]) + " " - else: - indent = sibling_indent + indent = _line_indent(source, slot[2]) + " " if sibling_indent is None else sibling_indent pair_key = _insert_pair(key, replacement, indent, eol, False) inserted = indent + pair_key + eol result = source[:at] + inserted + source[at:]